2010-08-10 113 views
1

我試圖使用mysqli來顯示數據,但它什麼也沒有顯示。mysqli結果集顯示爲null

我的代碼有什麼問題?

PHP類:

/* 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)和數據的語句是有在結果,因爲它並沒有去別的一部分,但我想不通爲什麼它不顯示任何內容。

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

回答

0

!具有比higher precedence ==,即

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

$query->num_rows邏輯否定(涉及一個內部 - >布爾鑄造),那麼這布爾值與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(); 
    } 
+0

$此 - > _ db是數據庫對象。我刪除了'try/catch',但它現在顯示'沒有引號'。 – input 2010-08-10 15:43:49

+0

「$ this - > _db是數據庫對象」是什麼樣的「數據庫對象」?一個(直接)mysqli的實例? 「我刪除了try/catch」 - 並添加了一些其他錯誤處理? – VolkerK 2010-08-10 15:59:34

+0

我已更新我的代碼,請檢查。 php文件包含連接到數據庫的基本文件。然後它創建該類的一個實例並調用所需的函數來顯示數據。我相信問題在於這一行'$ query = $ this - > _ db-> prepare($ sql);' – input 2010-08-10 16:32:06