Fatal error: Call to undefined method mysqli_result::rowCount()

Fatal error: Call to undefined method mysqli_result::rowCount()

我从 MYSQL 切换到 MYSQLi 时遇到问题。这些代码在 MYSQL 上工作正常,但是当我将连接更改为 MYSQLi 时,我在获取查询时收到了如上所述的错误。我如何使用 mysqli 函数获取我的查询?

代码:

        function __construct(){
    $this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating');

    if (!$this->link) {
     die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
    }

    echo 'Success... ' . mysqli_get_host_info($this->link) . "\n";

                }

    function getItems($id = null){
        if(isset($_GET['id']))
        {
            $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
        }
        else
        {
            $query = $this->link->query("SELECT * FROM items");
        }
        $rowCount = $query->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else
        {
            $result = 0;
        }
        return $result;

使用num_rows

阅读MySQLi row_count

所以最终答案是

function getItems($id = null)
{
    if(isset($_GET['id']))
    {
        $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
    }
    else
    {
        $query = $this->link->query("SELECT * FROM items");
    }
    $rowCount = $query->num_rows;//change here
    if($rowCount >= 1)
    {
        $result = $query->fetchAll();
    }
    else
    {
        $result = 0;
    }
    return $result;
}

编辑 01

使用mysqli_fetch_all代替fetchAll()

mysqli_fetch_all($query,MYSQLI_ASSOC);

所以回答世界是

    if($rowCount >= 1)
    {
        $result = mysqli_fetch_all($query,MYSQLI_ASSOC);
    }

您可以使用 num_rows 来计算数据库中的行,您可以像这样直接调用连接:-

//for connection

$con = mysqli_connect("localhost", "root", "", "ajax_rating");
if(!$con)
{
echo "connection error";
}


//query
function getItems($id = null)
 {
if(isset($_GET['id']))
{
    $query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'");
}
else
{
    $query = mysqli_query($con,"SELECT * FROM items");
}
if (mysqli_num_rows($query) > 0)//change here
    {
    while($row = mysqli_fetch_assoc($query))
     {
       $result=$row;
     }

}
else
{
    $result = 0;
}
return $result;
}