Jump to content

Using store_result() and use_result()


Cine

Recommended Posts

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();
    }

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
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.