Neha Bajpai
Neha Bajpai

Reputation: 167

num_rows not working in php/mysqli

  1. I am trying to get particular row one by one like this :

    while($getRowCount = $query->fetch_assoc()){
    
    $uid = $getRowCount['ID'];
    
  2. Next, I am selecting the row where this correspondingly matches in the second table as below:

    $getFullName = $db->prepare("SELECT * from registered_users where id = ?");
    $getFullName->bind_param("i",$uid);
    $getFullName->execute();
    if(($getFullName = $getFullName->num_rows) == 1){
        echo 'code reach';
        $getname = $getFullName->fetch_assoc();
        $FirstName = $getname['first_name'];
        $LastName = $getname['last_name'];
        echo '
          <br/>   
          <div id ="connect" style="font-weight:bold; font-size: 15px; color: #CF0B05;">
          <strong>'.$FirstName.' '.$LastName.'</strong>
          </div><hr/>';                                
     }
    
     }
    

The issue is num_rows == 1 does not return true despite of there being a match in the table for this row.

Also, if I forcefully evaluate to num_rows == 0, it echoes out 'code reach' but attaches a couple errors with itself. I do not get the point why num_rows == 1 does not return true despite there being a row.

 Notice: Trying to get property of non-object in /var/www/html/pages.php on line 634

 Fatal error: Call to a member function fetch_assoc() on integer in /var/www/html/pages.php on line 635

Any suggestions on how to resolve this would be of great help.

Upvotes: 1

Views: 1141

Answers (2)

Siddharth Mahajan
Siddharth Mahajan

Reputation: 53

replace your if condition

if(($getFullName = $getFullName->num_rows) == 1)

with

if((getFullName = mysqli_num_rows($getFullName)) == 1)

for more about mysqli_num_rows function you can visit click here

Upvotes: 0

Devon Bessemer
Devon Bessemer

Reputation: 35337

Look at your expression: ($getFullName = $getFullName->num_rows) == 1

You are first setting $getFullName equal to the number of rows (integer) then seeing if it equals 1. Then you later try to access $getFullName as an object:

$getname = $getFullName->fetch_assoc();

So of course you get the error:

 Fatal error: Call to a member function fetch_assoc() on integer in /var/www/html/pages.php on line 635

Why are you setting $getFullName equal to the number of rows? This is a simple case of understanding what each line of your code actually does.

Upvotes: 1

Related Questions