Old 08-08-2007   #1 (permalink)
Steven
 
Join Date: Jun 2005
Location: McAlester, OKlahaoma - United States
Posts: 21

Error on line 9...

I was wondering why I get this error

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/www/cwb.awardspace.com/faq/class.php on line 9

from the followingt code

PHP Code:
<?
class mysql {
    function 
Connect($dbhost$dbuser$dbpass$dbname){
        
$connection mysql_connect($dbhost$dbuser$dbpass);
        
mysql_select_db($dbname$connection);
    }
    
    function 
Close(){
        
mysql_close($this->connection);
    }
    
    function 
Query($sql){
        
$query mysql_query($sql) or die(mysql_error());
        return 
$query;
    }
    
    function 
GetNum($query){
          
$num mysql_num_rows($query);
          return 
$num;
      }
 
    function 
Fetch($query){
          
$array mysql_fetch_array($query);
          return 
$array;
      }
}
?>
__________________
--
Steven
basicwe is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 08-08-2007   #2 (permalink)
Moderator
 
Join Date: Jul 2004
Location: Quebec City, Canada
Posts: 50

When you write a member function in PHP, you cannot implicitly access the objet's scope. Instead, you have to explicitly refer to the variables using $this. Try this and see if it works better:


PHP Code:
<?

class mysql
{
    var 
$connection;

    function 
Connect($dbhost$dbuser$dbpass$dbname)
    {
        
$this->connection mysql_connect($dbhost$dbuser$dbpass);
        
mysql_select_db($dbname$this->connection);
    }
    
    function 
Close()
    {
        
mysql_close($this->connection);
    }
    
    function 
Query($sql)
    {
        
$query mysql_query($sql) or die(mysql_error());
        return 
$query;
    }
    
    function 
GetNum($query)
    {
          
$num mysql_num_rows($query);
          return 
$num;
    }
 
    function 
Fetch($query)
    {
          
$array mysql_fetch_array($query);
          return 
$array;
    }
}

?>
The Eagle is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 08-09-2007   #3 (permalink)
Steven
 
Join Date: Jun 2005
Location: McAlester, OKlahaoma - United States
Posts: 21

Please

Could you please explain $this in a little bit greater detail so I'll kniw next time what to do?
__________________
--
Steven
basicwe is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 08-09-2007   #4 (permalink)
Moderator
 
Join Date: Jul 2004
Location: Quebec City, Canada
Posts: 50

Hi,

In PHP and most other languages that I know of, objects are merely an aggregation of data (variables) in memory. Consider this example:

PHP Code:

class Person
{
    var 
$firstName;
    var 
$lastName;

    function 
printName()
    {
        echo 
$this->firstName " " $this->lastName;
    }

    function 
Person($firstName$lastName)
    {
        
$this->firstName $firstName;
        
$this->lastName $lastName;
    }
};

$johnSmith = new Person('John''Smith');
$janeDoe = new Person('Jane''Doe');

$johnSmith->printName();    /* Print: John Smith */
$janeDoe->printName();      /* Print: Jane Doe */ 
When you create an object of type Person ($johnSmith), PHP allocates memory space for two variables, $firstName and $lastName (and some metadata too, but this is not relevant). Both $johnSmith and $janeDoe have their own first and last names, but that's all. The functions themselves are not copied however, and only one instance of each function is shared for all Person objects. Here is a simplified snapshot of what the memory could contain:

["John" "Smith" ... "Jane" "Doe" ... [Shared code for printName] ...]

Thus, later, when the printName function is called, it has no way to know by itself whether the call comes from $johnSmith or $janeDoe (i.e. it has no clue which object it "belongs" to). Thankfully, PHP maintains the special variable $this that holds a reference to the object that made the call. This provides you with a way to reference any of the other class variables or functions that "belong" to the current object. Essentially, $this is a hint that tells PHP where to find the current object, and $this->firstName is a way to say "go get my first name."

Some other languages will do this automatically without you needing to reference the $this keyword, but PHP does not. Basically, always use $this-> when referring to anything that belongs to a specific object.
The Eagle is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 08-10-2007   #5 (permalink)
Steven
 
Join Date: Jun 2005
Location: McAlester, OKlahaoma - United States
Posts: 21

Wow

Thanks, Eagle, I really appreciate the help.
__________________
--
Steven
basicwe is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you create dotted line along selection border? aquastealth Adobe Photoshop 2 06-27-2007 07:27 PM
Macronimous- Command Line Scripting in PHP macro123 HTML / PHP / ASP / JS 1 03-30-2007 02:50 AM
Newbie need help to make 1px lines with line tool aquastealth Adobe Photoshop 15 09-15-2006 08:53 PM
diagonal line background.. ecntrc Adobe Photoshop 2 11-28-2005 03:34 PM
Line phunk (or funk...) ahmedtheking Adobe Photoshop 11 08-08-2004 04:00 PM


All times are GMT +1. The time now is 08:36 AM.
Content Relevant URLs by vBSEO 3.2.0 RC7

Design & Content © BioRUST 2007 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT