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';
}
$此 - > _ db是數據庫對象。我刪除了'try/catch',但它現在顯示'沒有引號'。 – input 2010-08-10 15:43:49
「$ this - > _db是數據庫對象」是什麼樣的「數據庫對象」?一個(直接)mysqli的實例? 「我刪除了try/catch」 - 並添加了一些其他錯誤處理? – VolkerK 2010-08-10 15:59:34
我已更新我的代碼,請檢查。 php文件包含連接到數據庫的基本文件。然後它創建該類的一個實例並調用所需的函數來顯示數據。我相信問題在於這一行'$ query = $ this - > _ db-> prepare($ sql);' – input 2010-08-10 16:32:06