Call to a member function on a non-object

Discussion in 'PHP' started by switto, Jun 23, 2008.

  1. #1
    hey, i'm working on a site that is entirely class based. This offers me many new possibilities, such as cleaner coding, implementation of templates and my very own constructed sandbox to create all the pages within.

    pretty much i have a main page called main.php which includes all of the classes
    also, it opens the sql connection by doing this:
    
    $db = new SecureSql;
    
    $db->user = SQL_USER_NAME;
    $db->password = SQL_USER_PASS;
    $db->host = SQL_HOST;
    $db->dbname = SQL_DATABASE;
    
    PHP:
    yea the connection works fine, dont worry. But now heres the problem: in another class called user, i call the function "query" which is in the SecureSql class i created. I call it like this:
    
    	function get_user($id) {
    	  $q = "SELECT * FROM users WHERE id = $id";
    	  $r = $db->query($q); // this is where the problem is at
    	  $this->info = mysql_fetch_array($r);
    	}
    
    PHP:
    this returns this error:
    so, how can i use one class function within another class?

    tia,
    -ajp
     
    switto, Jun 23, 2008 IP
  2. rockinaway

    rockinaway Guest

    Best Answers:
    0
    #2
    Try this:

    
    function get_user($id) 
    {
          global $db;
          $q = "SELECT * FROM users WHERE id = $id";
          $r = $db->query($q); // this is where the problem is at
          $this->info = mysql_fetch_array($r);
        }
    
    Code (markup):
     
    rockinaway, Jun 23, 2008 IP
  3. switto

    switto Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow I didn't have enough coffee this morning.... thanks for the help.
     
    switto, Jun 23, 2008 IP