views:

29

answers:

1

i've been trying my hand at mysqli.

using procedural mysql, the code displays the results fine. however, after making the switch to OOP mysqli, i tried converting the code to mysqli, and it says there are no quotes. is there anything wrong with the code?

/* the database object */
        private $_db;

        public function __construct($db=NULL)
        {
            if(is_object($db))
            {
                $this->_db = $db;
            }
            else
            {

                $this->_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            }
        }

        public function displayQuotes()
        {
            $sql = "SELECT cQuotes, vAuthor, cArabic, vReference 
                          FROM thquotes 
                          ORDER BY RAND()
                          LIMIT 1;";

                  try {

                      $query = $this->_db->prepare($sql);
                      $query->execute();
                      $query->store_result();

                      /* bind variables to prepared statement */
                      $query->bind_result($cQuotes, $vAuthor, $cArabic, $vReference);


                      if(!$query->num_rows==0)
                      {
                        while($row = $query->fetch())
                        {
                            //echo $this->formatQuotes($row);
                            $formatHTML = new formatHTML();
                            echo $formatHTML->formatQuotes($row);
                        }


                      }
                      else
                         {
                            echo "There are no Quotes!";
                         }
                       $query->free_result();
                      $query->close();
                    }
                  catch (Exception $ex){

                        echo "Something went wrong " . $ex;
                    }

        }
A: 

You didn't bind variables to resultset columns

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

Mchl
i tried binding just before checking if the number of records is 0 like this: `$stmt->bind_result($col1, $col2, $col3, col4);` and it still displayed 'no Quotes'. is this the correct way? or am i missing something?
fuz3d
Change `if(!$query->num_rows()==0)` to `if(!$query->num_rows==0)`It's a property, not a method.
Mchl
did that. still doesn't work. i've updated the code with the changes. please check.
fuz3d
Ahh... you need to store result before you can count rows:http://www.php.net/manual/en/mysqli-stmt.store-result.php
Mchl
i ran the debugger, and it shows the $query as null. why isn't it reading the data?
fuz3d
updated the code again. the error logs also don't show anything.
fuz3d