Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
using a class inside a function
Old 02-08-2009, 07:30 AM using a class inside a function
Average Talker

Posts: 27
Trades: 0
hi all,
i've been using a class for my database functions, example:

PHP Code:
    Class Query {
        var 
$action;
        var 
$connect;

        function 
Query($query,$sql) {
            
$this->action mysql_query($query,$sql);
            
$this->connect $sql;
        }
        
        function 
Error() {
            if(
mysql_errno($this->connect))
                return 
'Error #' mysql_errno($this->connect) . ': ' mysql_error($this->connect) . '<br />';
            else
                return 
false;
        }

    function 
FetchAssoc() {
        return 
mysql_fetch_assoc($this->action);
    }

        function 
Free(){
            if(
is_resource($this->action) && get_resource_type($this->action) == 'mysql link'){
                
mysql_free_result($this->action);
            }
        }

    function 
Close($sql){
        
mysql_close($sql);
    }


    } 
there are also many more functions inside the class, but i have only listed a few. in my script, if i apply a query using this class for example:

PHP Code:

    $selectQuery 
'SELECT * FROM `sample_table` WHERE `id` = 3 LIMIT 1';

    
$result = new Query($selectQuery,$sql);
    if(
$result->Error()) echo $result->Error();
    else {
        
// print our results
        
$row $result->FetchAssoc();
        echo 
"id is: " $row['id'] . "<br />\n";
        echo 
"name is: " $row['name'] . "<br />\n";
        
$result->Free();
        unset(
$row);
    }

    
$result->Close($sql); 
it all works fine. however i would like to write put that into a function such as called displayAllSamples(). the problem i have is that as soon as i do i get errors such as:

'mysql_query(): supplied argument is not a valid MySQL-Link resource in...'


for each time where it tries to use the functions from inside the class. i know the code for applying query is fine as it works if i don't use it in a function. how do i adapt my code/class/functions so it can be put into a new function?

thanks
gavrd1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-09-2009, 07:14 AM Re: using a class inside a function
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Im going to guess that the $sql variable isn't present or doesnt have the right value within the function. Try passing it in as a parameter.
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to using a class inside a function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12853 seconds with 12 queries