Jump to content

Recommended Posts

Hi guys. I put this code together to connect to MySQL using PHP Classes. The connection worked beautifully. I then created another class to process the fetch_assoc, etc. I keep getting this error however:

Fatal error: Call to a member function fetch_assoc() on a non-object in XXXX

 

I'm pretty much going to explode after hours of debugging with no results.

 

Here's my code:

 

//Now lets throw the classes
//But first, let's associate the connection to the db

Class Db_connection {
protected $db_user;
protected $db_pass;
protected $db_host;
protected $db_name;
protected $db;

public function __construct($db_host, $db_user, $db_pass, $db_name)
{
   $this->db_host = $db_host;
   $this->db_user = $db_user;
   $this->db_pass = $db_pass;
   $this->db_name = $db_name;
}

public function connect()
{
   $this->db = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
   if(!is_resource($this->db)) {
     throw new Exception;
   }
   if(!mysql_select_db($this->db_name, $this->db)) {
     throw new Exception;
   }
}

public function query($query) {
   if(!$this->db) {
     $this->connect();
   }
   //throw the result "rest"
   $rest = mysql_query($query, $this->db);
   if(!$rest) {
     throw new Exception;
   }
   elseif (is_resource($rest)) {
     return TRUE;
   }
   else {
     //throw the statement and process the query
     $stmt = new Db_mysql_statement($this->db, $query);
     $stmt->result = $rest;
     return $stmt;
   }
}
}

//fetch the MySQL rows
Class Db_mysql_statement{
  protected $result;
  public $query;
  protected $db;
  
  public function __construct($db, $query)
  {
    $this->query = $query;
    $this->db = $db;
    if(!is_resource($db)) {
      throw new Exception("Need new class to log error [40: No DB Connection!]");
    }
  }
  
  public function fetch_row()
  {

    if(!$this->result) {
      throw new Exception("Neeed new class to log error [50: Query Not Executed!]");
    }
    return mysql_fetch_row($this->result);
  }
  
  public function fetch_assoc()
  {
    return mysql_fetch_assoc($this->result);
  }
}

 

Here's where I create a query:

 

//connect to the MySQL database
$db = New Db_connection($db_host, $db_user, $db_pass, $db_name);

$query = $db->query("SELECT * FROM users");
$row = $query->fetch_assoc();
$user = $row['username'];

 

Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/
Share on other sites

The object

$query->

has not been created to access the functions in class Db_mysql_statement

 

I thought I defined it here:

 


Class Db_mysql_statement{
  protected $result;
  public $query;
  protected $db;

  public function __construct($db, $query)
  {
    $this->query = $query;
    $this->db = $db;
//........

 

Excuse my ignorance. I've just grasped the OOP way of developing in PHP after a long time of developing in standard form.

Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726765
Share on other sites

It would be better to have  a separate class for the connection and queries.

You have created the $db object for the connection class correctly.

So for the second class which will contain all the functions for a query create an object like this:

  $query =new Db_mysql_statement();

 

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726770
Share on other sites

The problem is with your query() method in your connection class.  You have some weird logic in there; namely, the is_resource() call on the mysql_query() result.  It'll always be a resource if the query was valid, so I think you should rework your logic.

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726773
Share on other sites

Thanks for your input guys, I'm definitely still in the learning process of OOP. Everyone's input and criticism are more then welcome since it helps me improve. :P

 

I'll definitely rebuild the classes again in a more neater way based on your comments and I'll post it up again here for further criticism.

Thanks a bunch!

Best Regards.

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726776
Share on other sites

Alright, so I rewrote this again, now the query and connect works, but for some reason the mysql_fetch_assoc doesn't work. Here's my new classes:

 

Class Db_connect {
protected $db_host;
protected $db_user;
protected $db_pass;
protected $db_name;
protected $db; //connection handler

public function __construct($db_host, $db_user, $db_pass, $db_name) 
{
   $this->db_host = $db_host;
   $this->db_user = $db_user;
   $this->db_pass = $db_pass;
   $this->db_user = $db_user;
}

public function connect()
{
   $this->db = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
   if (!$this->db) 
   { 
     die(" Encountered an Error as Follows: Error: [40]
          Please contact us with the error number");
   }
   return mysql_select_db($this->db_name, $this->db);
}

public function query($query) 
{
   if(!$this->db)
   {
     $this->connect();
   }
   
   //now process the queries
   $result = mysql_query($query, $this->db);
   
   if(is_resource($result)) 
   {
     return TRUE;
   }
   
   //now process the statement
   else 
   {
     $stmt = new Mysql_statement($this->db, $query);
     $stmt->result = $result;
     return $stmt;
   }
}
}

Class Mysql_statement Extends Db_connect {
  protected $db;
  protected $result;
  public $query;
  
  public function __construct($db, $query)
  {
    $this->db = $db;
    $this->query = $query;
    if(!is_resource($db)) 
    {
      Throw New Exception("Encountered an Error as Follows: Error: [41]
                           Please contact us with the error number");
    }
  }
  
  public function fetchRows()
  {
    if(!$this->result)
    {
      Die("Encountered an Error as Follows: Error: [42]
                           Please contact us with the error number");
    }
    return mysql_fetch_row($this->result);
  }
  
  public function fetchAssoc()
  {
    if(!$this->result)
    {
      Die("Encountered an Error as Follows: Error: [43]
                           Please contact us with the error number");
    }
    return mysql_fetch_assoc($this->result);
  }
}

 

Here's where I connect to everything and create a query:

 

//let's initaite the database information to be used for db connection
$db_host = 'xxxx';
$db_user = 'xxxx';
$db_pass = 'xxxx';
$db_name = 'xxxx';

//connect to the MySQL database
$db = new Db_connect($db_host, $db_user, $db_pass, $db_name);

$query = $db->query("SELECT * FROM users");

while($row = $query->fetchAssoc()) {
  $user = $row['username'];
  echo $user;
}

 

Any help would be greatly appreciated. The error I'm getting is the set 43 error. I don't think the query is being properly set. :-/

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730347
Share on other sites

public function query($query) 
{
   if(!$this->db)
   {
     $this->connect();
   }
   
   //now process the queries
   $result = mysql_query($query, $this->db);
   
   if(is_resource($result)) 
   {
     return $result;
   }
   
   //now process the statement
   else 
   {
     $stmt = new Mysql_statement($this->db, $query);
     $stmt->result = $result;
     return $stmt;
   }
}

 

return $result instead of return TRUE

Link to comment
https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730408
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.