phpmysql-num-rowsmysqlnd

Php $num_rows not working after installing Mysqlnd


I'm trying to get my $num_rows working. It had been working, and now it's not. Reading this site it was suggested I install MySqlnd on my server because some things like fetch_array and get_result were not working and now it's like I wish I'd never installed it. (Should I uninstall it?)

I've been trying so many variations of things to try get $num_rows working - and other functions - I don't know what is right or wrong anymore. Can you help ? The error I am getting is :

Fatal error: Call to undefined method mysqli::stmt() in /var/www/html/checkcontact.php on line 34

Line 34 is : $result = $con->stmt($query);

Here's my code :

<?php

require('dbConnect.php');

//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
//decode the JSON
$array = json_decode($json);
//bind 
 $query = "SELECT * FROM user WHERE username = ?";
 $stmt = $con->prepare($query) or die(mysqli_error($con));
 $stmt->bind_param('s', $phonenumber);

 //for each value of phone_number in the array, call it $phonenumber
    foreach ($array as $value)
    {
        $phonenumber = $value->phone_number;

$stmt->execute();
$result = $con->stmt($query);
//$result = $stmt->get_result(); 
//$stmt->store_result();
//$num_rows = $stmt->num_rows();
 //$result = mysqli_query($con, $query);

 if ($result->$num_rows > 0) {
    //  while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    // while($row = mysqli_fetch_assoc($result)){ 

    // $contact_id = $row['user_id'];
    // echo $contact_id;

    echo "number of phone numbers in the user table is " . $num_rows  . "<br>"; 
     }
 }
 ?>

EDIT : Here is my dbConnect.php file, as requested :

<?php

define ('HOST', 'localhost');
define ('USER', 'bob');
define ('PASS', 'password');
define ('DB', 'mydb');

$con = NEW MySQLi("localhost", "bob", "password", "mydb");

if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
} 

?>

Solution

  • Your first error is this line

    $result = $con->stmt($query);
    

    You probably want to get_result() instead.

    There is no mysqli::stmt() method. Then, there's no $result->$num_rows, but it's a property of the result, which means it should be $result->num_rows (loose the $).

    Revised with those changes, this is most likely how your code should look like.

    //post all contacts in my phone as a JSON array
    $json = $_POST['phonenumber'];
    //decode the JSON
    $array = json_decode($json);
    //bind 
    $query = "SELECT * FROM user WHERE username = ?";
    $stmt = $con->prepare($query) or die($con->error);
    $stmt->bind_param('s', $phonenumber) or die ("MySQLi-stmt binding failed ".$stmt->error);
    
    //for each value of phone_number in the array, call it $phonenumber
    foreach ($array as $value) {
        $phonenumber = $value->phone_number;
        $stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
    
        if ($stmt->num_rows > 0) { // Check if there are any rows matching this value
            $result = $stmt->get_result(); // Convert from MySQLi_stmt to MySQLi_result (to use fetch_assoc())
            echo "Number of rows matching username '".$value->phone_number."' from user-table is " . $result->num_rows  . " rows.<br>"; 
            while ($row = $result->fetch_assoc()) {
                echo $row['user_id']."<br />";
            }
        } else {
            echo "No rows matching username ".$value->phone_number.".<br />";
        }
    }
    $stmt->close();
    

    Note that the mysqli_stmt::get_result() method requires the mysqlnd driver. If you don't have that driver, you will have to fetch the results differently, using mysqli_stmt::bind_result() and mysqli_stmt::fetch() (aside from that, installing that driver - if installed correctly - won't affect your code).