Cine Posted July 29, 2011 Share Posted July 29, 2011 Hi there, I have a Database Abstraction class for MySQL. I'm trying to use the MySQLi::store_result() to save a result set returned by a successful SELECT query (MySQLi::query()). As soon as that is done (Or at any time), I call the member function getData() which I hoped would return an associative array of the stored SELECT data. However, I'm not sure if I'm doing this correctly at all. The code at present, is returning a Fatal error: Fatal error: Call to a member function fetch_assoc() on a non-objectFatal error: Call to a member function fetch_assoc() on a non-object The $data variable is being passed the result of calling use_result(), which is supposed to be an unbuffered result object. Currently however, it's returning False as something erroneous occurred. But I have no idea what. I've posted an extract of the class below. Thanks in advance. public function select($table, $fields, $condition = false) { if($this->isConnected()) { if(isset($table, $fields)) { $SQL = "SELECT "; foreach($fields as $column) { $SQL .= "`$column`"; $SQL .= ($column != end($fields)) ? ", " : " "; } $SQL .= "FROM $table "; $SQL .= ($condition) ? "WHERE $condition" : ""; $result = $this->query($SQL); if($result->num_rows > 0) { $this->DB->store_result(); return $this->getData(); } return 0; } else { echo self::ERR_SELECT_SYNTAX; return false; } } } public function getData() { $data = $this->DB->use_result(); return $data->fetch_assoc(); } Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/ Share on other sites More sharing options...
Cine Posted July 29, 2011 Author Share Posted July 29, 2011 No-one? Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249215 Share on other sites More sharing options...
trq Posted July 29, 2011 Share Posted July 29, 2011 Have you tried checking $this->DB->error() ? Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249216 Share on other sites More sharing options...
Cine Posted July 29, 2011 Author Share Posted July 29, 2011 Yeah, but there is no error. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249222 Share on other sites More sharing options...
Cine Posted July 29, 2011 Author Share Posted July 29, 2011 Im thinking it HAS to be something to do with how I'm using save_results() and use_result(). But after scouring through the PHP docs, I've yet to come across anything that alludes to this. EDIT: Yeah, the store_result() function is returning false, which means an error occurred. Which explains why use_result() is returning an error. Now to find why it's failing? Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249223 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2011 Share Posted July 29, 2011 The mysqli->query() method, by default, performs a store_result() (transfers the result set from the database server and buffers it in memory.) There's no point in calling store_result() after using the mysqli->query() method. You also wouldn't use both store_result() and use_result() (leaves the result set on the database server thereby tying up the server and preventing other threads from updating any tables from which the data is being fetched) on the same result set. Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249233 Share on other sites More sharing options...
Cine Posted July 29, 2011 Author Share Posted July 29, 2011 PFMaBiSmAd, thanks. Simply assigning the result from the query to some member variables works fine. Quote Link to comment https://forums.phpfreaks.com/topic/243205-using-store_result-and-use_result/#findComment-1249235 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.