2014-01-19 14 views
-2

我有改變的mysql_query聲明的mysqli如下如何使用mysqli查詢而不是mysql_query沒有得到致命錯誤?

$sqlordlod = "SELECT * FROM order_list 
      WHERE user_id = '$user_id' 
      ORDER by order_id LIMIT $offset, $rec_limit "; 
$result = $mysqli->query($sqlordlod); 
$countrw = $result->num_rows; 
echo $countrw; 

數據庫連接文件

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database); 

/* check connection */ 
if (mysqli_connect_errno()) { 
printf("Connect failed: %s\n", mysqli_connect_error()); 
exit(); 
} 

反正跟着我做了上述修改建議,但得到:

Warning: mysqli::query() expects parameter 1 to be string, object given in /Applications/XAMPP/xamppfiles/htdocs/_/globe/ru/profile.php on line 487

Fatal error: Call to a member function fetch_array() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/_/globe/ru/profile.php on line 487

下面是線487

while($rowld = $mysqli->query($result)->fetch_array()) 
{ 
// flip flop controling the tr class to change the color 
if ($classchk ==3){ 
     $classchk =1; 
    } 
if ($classchk ==2){ 
    $classname = "alt"; 
}else{ 
    $classname = "none"; 

    } 
+0

你能提供更多的代碼,所以我可以理解你想達到什麼嗎? – vooD

回答

1

num_rows是一個屬性,而不是一個方法:

$countrw = $result->num_rows; 
          ^--- no function call 
+0

沒有函數調用?它會錯誤的,不是嗎? –

+0

沒有。這不是一種方法。它是mysqli語句對象的一個​​PROPERTY。 –

+0

是的你是對的,我已經糾正它。 –

0

更換

$result = $mysqli->query($sqlordlod); 

$result = $mysqli->query($serverconnection,$sqlordlod); 

這裏$一個ServerConnection是你的數據庫連接結果

試試這個一個

這裏是計數行

$dbhost="localhost"; 
$dbusername="root"; 
$dbpassword=""; 
$dbname="test"; 

$con=mysqli_connect($dbhost,$dbusername,$dbpassword,$dbname); 

/* check connection */ 
$sqlordlod = "SELECT * FROM tablename"; 
//$resultordlod = mysql_query($sqlordlod); 
//$countrw = mysql_num_rows($resultordlod); 
$result = mysqli_query($con, $sqlordlod); 
// fetch the result row. 
$countrw = mysqli_num_rows($result); 
echo $countrw; 
+0

我已經將db連接腳本添加到問題中,請看看這個問題。我已經將查詢更改爲$ result = $ mysqli-> query($ mysqli,$ sqlordlod);你是這個意思嗎?如果是,那麼我得到一個新的錯誤警告:mysqli ::查詢()期望參數1是字符串,對象在/Applications/xamppfiles/h....../prole.php行461 –

+0

請寫461在這裏行 – Vivek

+0

-1。對象模式下的mysqli不需要連接句柄,因爲該句柄內置於對象中。事實上,你的方法調用是完全錯誤的,因爲**沒有參數可以通過處理** –

1

NUM_ROWS容易和工作代碼屬性不是方法。

$sqlordlod = "SELECT * FROM order_list 
       WHERE user_id = '$user_id' 
       ORDER by order_id LIMIT $offset, $rec_limit "; 
$result = $mysqli->query($sqlordlod); 
$countrw = $result->num_rows; 
echo $countrw; 

編輯:你的代碼真的有問題。我想你想做以下事情:

while($rowld = $result->fetch_array()) 
{ 
// flip flop controling the tr class to change the color 
if ($classchk ==3){ 
     $classchk =1; 
    } 
if ($classchk ==2){ 
    $classname = "alt"; 
}else{ 
    $classname = "none"; 

    } 
+0

嗨感謝您的更正,但有新的錯誤,我修改了問題,並保存錯誤消息在問題的身體長期與代碼。請再次看看帖子,然後再無緣無故地再次投票:D謝謝。 –

+0

編輯我的答案 – vooD