tags:

views:

25

answers:

1

i'm trying to use mysqli to display data, but it displays nothing.

what is wrong with my code?

php class:

/* 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;";



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



        }

it does read the statement of if(!$query->num_rows==0) and data is there in the resultset since it does not go to the else part, but i cannot figure out why it isn't displaying anything.

php file:

include "base.php";
include_once "inc/class.quotes-m.inc.php";
$quotes = new Quotes($db);

$quotes->displayQuotes();

php base.php:

include_once("inc/constants.inc.php");

error_reporting(E_ALL);
ini_set("display_errors", 1);



        $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

         if (!$db) {
            echo 'db link fail';
        }
A: 

! has a higher precedence than ==, i.e.

if(!$query->num_rows==0) { ... }
// is equivalent to
if( (!$query->num_rows) == 0 ) { ... }

i.e. $query->num_rows is logically negated (involving an int->bool cast) then this boolean value is compared to 0.

But you want something like

if( 0 < $query->num_rows ) { ... }

You're using a try { ... } catch() block. But the mysqli extension doesn't throw exceptions (IIRC). Unless $this->_db is some kind of (additional) wrapper you have to test the return values of the mysqli methods, or use an api that actually throws an exception when an error occurs like e.g. PDO (when setting the error mode to PDO::ERRMODE_EXCEPTION).


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

    $query = $this->_db->prepare($sql);
    if ( !$query ) {
      throw new ErrorException($this->_db->error, $this->_db->errno);
    }
    $r = $query->execute();
    if ( !$r ) {
      throw new ErrorException($query->error, $query->errno);
    }

    $r = $query->store_result();
    if ( !$r ) {
      throw new ErrorException($query->error, $query->errno);
    }

    /* bind variables to prepared statement */
    $r = $query->bind_result($cQuotes, $vAuthor, $cArabic, $vReference);
    if ( !$r ) {
      throw new ErrorException($query->error, $query->errno);
    }

    if( 0 < $query->num_rows ) {
      $formatHTML = new formatHTML();
      while( false!==($row=$query->fetch()) ) {
        echo $formatHTML->formatQuotes($row);
      }
    }
    else {
      echo "There are no Quotes!";
    }
    $query->free_result();
    $query->close();
  }
VolkerK
$this->_db is the database object. i removed `try/catch` but it now displays 'no Quotes'.
fuz3d
"$this->_db is the database object" what kind of "database object"? A (direct) instance of mysqli? "i removed try/catch" - and added some other error handling instead?
VolkerK
i've updated my code, please check. the php file includes the base file which connects to the database. it then creates an instance of the class and calls the required function to display the data. i believe the problem lies with this line `$query = $this->_db->prepare($sql);`
fuz3d
Still no error handling. All of those mysqli method can fail. Test the return values and if they indicate an error do something. E.g. throw an exception containing the error message, see http://docs.php.net/mysqli.error
VolkerK
@volkerk, can you give me an example on how i should go about handling the error in this case? i tried the examples given in that link, but it doesn't throw any error; just displays that there are no quotes.
fuz3d
@VolkerK, thanks, and it still isn't displaying any error. Now, it's crashing my browser as well. This is the error in the error log: `PHP Fatal error: Maximum execution time of 30 seconds exceeded in \inc\\class.quotes-m.inc.php on line 58`
fuz3d