3

This is my first experience with mysqli. It seems to be looking for the name of a result set in between the parentheses in mysqli_num_rows(). Yet when I tried $stmt, $conn, and nothing, I got the same error. Frustrating! What goes where $WHAT is in the last line below?

Or maybe I'm trying the wrong tack. All I want to do is check that a result was returned. I don't really need the number of rows. Should I just do an else statement with an error message? Is that the best way to do it? And is there a good way to write a function to connect and accept the query and it's parameters? I wrote one for mysql but this is so different! I'm not looking forward to rewriting dozens of queries!

$conn = mysqli_connect($host, $user, $pwd, $db,$port=$port_nbr); 

if ($mysqli_connect_errno) {
    printf("Connect failed: %s\n",
    mysqli_connect_error());
    exit;
}
if($stmt=$conn->prepare("SELECT id, name, status, type FROM organization")) {
    $stmt->execute();
    $stmt->bind_result($org_id, $orgname, $orgstatus, $orgtype);    
    $num=mysqli_num_rows($WHAT);
}
dac
  • 811
  • 3
  • 12
  • 24
  • [`mysqli_num_rows()`](https://www.php.net/manual/en/mysqli-result.num-rows.php) expects a result set identifier, but [`mysqli_prepare()`](https://www.php.net/manual/en/mysqli.prepare.php) returns a statement object. Compare [`mysqli_num_rows()`](https://www.php.net/manual/en/mysqli-result.num-rows.php) and [`mysqli_stmt_num_rows()`](https://www.php.net/manual/en/mysqli-stmt.num-rows.php). It might be confusing that both use a similar object oriented syntax: `$mysqli_result->num_rows` and `$mysqli_stmt->num_rows`. – showdev Aug 02 '19 at 07:17

3 Answers3

8

PHP can return the number of rows only if the result set returned by the query is buffered on the PHP side. But prepared queries are unbuffered by default. So in order to get the number you need to call the get_result() method first:

$stmt = $conn->prepare("SELECT id, name, status, type FROM organization WHERE id=?");         
$stmt->bind_param("s", $id);    
$stmt->execute();
/ *** HERE ***/
$result = $stmt->get_result();
$num = $result->num_rows();

but in reality you don't actually need that number. Given you will fetch the selected data anyway, using either fetch_assoc() or fetch_all() methods, you can always use that data instead of $num.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
shmeeps
  • 7,725
  • 2
  • 27
  • 35
  • No problem. Just remember, while you can intermix the two, it's usually better and less troublesome to pick one method and stick with it! – shmeeps Apr 23 '11 at 04:08
2

mysqli_num_rows takes the query result as a parameter http://us.php.net/manual/en/mysqli-result.num-rows.php

You could also use it in an OOP style as $result->num_rows;

Brian
  • 1,663
  • 1
  • 14
  • 11
-1

The reason for this error is that you may be passing the $stmt object to mysqli_num_rows().

Moreover, what I see is that you're mixing up two different approaches in php for using mysql databases - the Object-Oriented Approach and the Procedural Approach. You have to chose one of them and then proceed something like this -

Procedural Approach (Using mysqli extension)

$con = mysqli_connect("localhost", "user", "pass", "test") or die("Connection Error: " . mysqli_error($conn));
$query = "SELECT id, name, status, type FROM organization"; 
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$count = mysqli_num_rows($result);

// $count now stores the number of rows returned from that query

Object-Oriented Approach (Using mysqli extension)

<?php
$host = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";

// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, name, status, type FROM organization";
$result = $conn->query($sql);
$count = $result->num_rows;
// $count now stores the number of rows returned
$conn->close();
?>

You can read more about it here - https://www.w3schools.com/php/php_mysql_select.asp