Imad Posted December 31, 2008 Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/ Share on other sites More sharing options...
suzzane2020 Posted December 31, 2008 Share Posted December 31, 2008 The object $query-> has not been created to access the functions in class Db_mysql_statement Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726761 Share on other sites More sharing options...
Imad Posted December 31, 2008 Author Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726765 Share on other sites More sharing options...
suzzane2020 Posted December 31, 2008 Share Posted December 31, 2008 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(); Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726770 Share on other sites More sharing options...
DarkWater Posted December 31, 2008 Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726773 Share on other sites More sharing options...
Imad Posted December 31, 2008 Author Share Posted December 31, 2008 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-726776 Share on other sites More sharing options...
Imad Posted January 5, 2009 Author Share Posted January 5, 2009 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. :-/ Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730347 Share on other sites More sharing options...
DarkWater Posted January 5, 2009 Share Posted January 5, 2009 You never set $this->result in the Mysql_statement class... Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730387 Share on other sites More sharing options...
Imad Posted January 6, 2009 Author Share Posted January 6, 2009 That's the thing, what would it equal? Thanks for your help, Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730401 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730408 Share on other sites More sharing options...
Imad Posted January 6, 2009 Author Share Posted January 6, 2009 Alright, I changed it to no avail. Any other ideas? Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/138954-php-class-help/#findComment-730414 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.