How to count returned rows number without using get_result() in mysqli?

1.1k Views Asked by At

I am using GoDaddy shared hosting plan and looks like there are some issues on using the mysqli and mysqlnd in all versions of PHP 7,7.1,7.2, and 7.3 on their hosting.

After many tries on changing and updating PHP versions and modules I, eventually, gave up using the get_result() function as I am getting this error

Uncaught Error: Call to undefined method mysqli_stmt::get_result()

What other function can I use instead of theget_result() to make sure there are rows in the $result?

if ($stmt = $conn->prepare($query)) {
            $stmt->execute();
            $result = $stmt->get_result();
            if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                 $items[] = $row;
             }
            $stmt->free_result();
            $stmt->close();
            }
            $conn->close();
        }
1

There are 1 best solutions below

3
Adam Hull On

Try using $stmt->num_rows and get rid of the get_result

 if ($stmt = $conn->prepare($query)) {
 $stmt->execute();
 if ($stmt->num_rows > 0) { 
while ($row = $stmt->fetch_assoc())         { 
 $items[] = $row;
 }
 $stmt->free_result(); $stmt->close(); 
 } 
$conn->close();
 }