mysqli结果集显示为null

时间:2010-08-10 13:56:33

标签: php oop mysqli

我正在尝试使用mysqli来显示数据,但它什么也没显示。

我的代码出了什么问题?

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



        }

它确实读取了if(!$query->num_rows==0)的语句,并且数据存在于结果集中,因为它没有进入else部分,但我无法弄清楚为什么它没有显示任何内容。

php文件:

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';
        }

1 个答案:

答案 0 :(得分:0)

!有一个higher precedence比==,即

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

即。 $query->num_rows在逻辑上被否定(涉及int-> bool强制转换),然后将此布尔值与0进行比较。

但你想要像

这样的东西
if( 0 < $query->num_rows ) { ... }

你正在使用try {...} catch()块。但是mysqli extension并没有抛出异常(IIRC)。除非$this->_db是某种(附加)包装器,否则你必须测试mysqli方法的返回值,或者使用实际上在发生错误时抛出异常的api,例如: PDO(将错误模式设置为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();
  }