如何使用mysqli查询而不是mysql_query而不会出现致命错误?

时间:2014-01-19 08:24:50

标签: php mysql mysqli

我已将mysql_query语句更改为mysqli,如下所示

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

无论如何,请遵循我做出上述修改的建议,但得到了:

  

警告:mysqli :: query()期望参数1为字符串,第487行/Applications/XAMPP/xamppfiles/htdocs/_/globe/ru/profile.php中给出的对象

     

致命错误:在第487行的/Applications/XAMPP/xamppfiles/htdocs/_/globe/ru/profile.php中的非对象上调用成员函数fetch_array()

以下是第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";

    }

3 个答案:

答案 0 :(得分:1)

num_rows是属性,而不是方法:

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

答案 1 :(得分: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";

    }

答案 2 :(得分: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;