Imad Posted January 9, 2009 Share Posted January 9, 2009 Alright, I gave my Mysql DB classes a bit of a revamp since last time. This time, I merged both classes into one and all seems smooth except for this MySQL error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in XXXX I'm assuming that the query function in my class didn't properly process the mysql_query function. However, I did specify my fetchAssoc function to do it if it hasn't been done already. Take a look at the class: Class Db_connect { protected $db_host; protected $db_user; protected $db_pass; protected $db_name; protected $result; 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); //let's check if we're connected if (!$this->db) { //since we're not connected, the given information must be incorrect die("Encountered an Error as Follows: Error: [40] Please contact us with the error number"); } //we know we're in. So why not select the db? return mysql_select_db($this->db_name, $this->db); } public function query($query) { if(!$this->db) { $this->connect(); } $query = mysql_query($query, $this->db); return $query; } public function fetchAssoc($query) { if(!is_resource($query)) { $this->query($query); } $assoc = mysql_fetch_assoc($query); return $assoc; } } Here's where I do everything (connection, query, etc): //let's initaite the database information to be used for db connection $db_host = 'localhost'; $db_user = 'xxx'; $db_pass = 'xxx'; $db_name = 'xxx'; //connect to the MySQL database $db = new Db_connect($db_host, $db_user, $db_pass, $db_name); $query = $db->query("SELECT * FROM users"); $row = $db->fetchAssoc($query); As you can see above, I used the query function for the query, I then used the fetchAssoc function using the query, expecting that it has been processed. Any help will be greatly appreciated. Best Regards. Quote Link to comment https://forums.phpfreaks.com/topic/140181-db-connection-error-using-classes/ Share on other sites More sharing options...
Brian W Posted January 9, 2009 Share Posted January 9, 2009 Error in you sql syntax? Can you change public function query($query) { if(!$this->db) { $this->connect(); } $query = mysql_query($query, $this->db); return $query; } to public function query($query) { if(!$this->db) { $this->connect(); } $query = mysql_query($query, $this->db) or die(mysql_error()."<br>".$query);//Should kill entire app if the query is bad. return $query; } Quote Link to comment https://forums.phpfreaks.com/topic/140181-db-connection-error-using-classes/#findComment-733552 Share on other sites More sharing options...
KevinM1 Posted January 9, 2009 Share Posted January 9, 2009 Alright, I gave my Mysql DB classes a bit of a revamp since last time. This time, I merged both classes into one and all seems smooth except for this MySQL error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in XXXX I'm assuming that the query function in my class didn't properly process the mysql_query function. However, I did specify my fetchAssoc function to do it if it hasn't been done already. Take a look at the class: Class Db_connect { protected $db_host; protected $db_user; protected $db_pass; protected $db_name; protected $result; 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); //let's check if we're connected if (!$this->db) { //since we're not connected, the given information must be incorrect die("Encountered an Error as Follows: Error: [40] Please contact us with the error number"); } //we know we're in. So why not select the db? return mysql_select_db($this->db_name, $this->db); } public function query($query) { if(!$this->db) { $this->connect(); } $query = mysql_query($query, $this->db); return $query; } public function fetchAssoc($query) { if(!is_resource($query)) { $this->query($query); } $assoc = mysql_fetch_assoc($query); return $assoc; } } Here's where I do everything (connection, query, etc): //let's initaite the database information to be used for db connection $db_host = 'localhost'; $db_user = 'xxx'; $db_pass = 'xxx'; $db_name = 'xxx'; //connect to the MySQL database $db = new Db_connect($db_host, $db_user, $db_pass, $db_name); $query = $db->query("SELECT * FROM users"); $row = $db->fetchAssoc($query); As you can see above, I used the query function for the query, I then used the fetchAssoc function using the query, expecting that it has been processed. Any help will be greatly appreciated. Best Regards. The problem lies here: public function fetchAssoc($query) { if(!is_resource($query)) { $this->query($query); // <--- right here } $assoc = mysql_fetch_assoc($query); return $assoc; } Your query function returns a mysql_result, yet you don't actually capure that result because you don't assign the return value to a variable. So, if the value passed into the function isn't a result, you're screwed, because mysql_fetch_assoc() is trying to fetch a result from the actual query text rather than a mysql_result. A solution would be: public function fetchAssoc($query) { if(!is_resource($query)) { $result = $this->query($query); } else { $result = $query; } $assoc = mysql_fetch_assoc($result); return $assoc; } Quote Link to comment https://forums.phpfreaks.com/topic/140181-db-connection-error-using-classes/#findComment-733555 Share on other sites More sharing options...
Imad Posted January 9, 2009 Author Share Posted January 9, 2009 Thanks for your reply. It returned as "Query was Empty". I'm quite puzzled here on why it's returning that. ??? EDIT: reading your new post. EDIT again: I get the Query empty thing I described above. Quote Link to comment https://forums.phpfreaks.com/topic/140181-db-connection-error-using-classes/#findComment-733556 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.