0

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

?>
2
  • It seems the fault is not in your $num_rows method. Can you post the dbConnect.php file? This is where your $con variable comes from right? (Dont forget to remove passwords!) Commented May 1, 2017 at 9:41
  • @Clemenz Thanks, edited post to include my dbConnect.php. Commented May 1, 2017 at 9:45

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

4 Comments

+1. No errors now but it goes to the else statement - No rows matching, even though there are clearly phone numbers in my JSON array that are in my user table. Any ideas ?
That shouldn't matter from the MySQL point of view, I'd start by checking that the values of $value->phone_number are exactly what you expect them to be.
Yes, your code just takes some tinkering, I'll figure it out. When I change it to if ($stmt->num_rows == 0) it echoes correctly, weird. Thanks for your help.
That means that there weren't any rows returned for that username, though. Weird, indeed. Hopefully it helped you on your way at least ;-) Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.