Jump to content

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boo


genzedu777

Recommended Posts

Hi all,

 

I have received a warning message, which it still puzzles me. I suspect it might be my inner join command, which I have coded it wrongly?

 

Line 37 refers to this line -  if (mysqli_num_rows($data) == 1) {

 

Do you guys have any idea? Thanks

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\inetpub\vhosts\123.com\http\viewprofile.php on line 37

 

<?php
  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  $query = "SELECT tp.name, tp.nric, tp.gender, tp.race_id, r.race_name AS race" .
        "FROM tutor_profile AS tp " .
        "INNER JOIN race AS r USING (race_id) " .
"WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
  
  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 1) {
    // The user row was found so display the user data
    $row = mysqli_fetch_array($data);
    echo '<table>';
    if (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    }
    if (!empty($row['nric'])) {
      echo '<tr><td class="label">NRIC:</td><td>' . $row['nric'] . '</td></tr>';
    }
    if (!empty($row['last_name'])) {
      echo '<tr><td class="label">Last name:</td><td>' . $row['last_name'] . '</td></tr>';
    }
    if (!empty($row['gender'])) {
      echo '<tr><td class="label">Gender:</td><td>';
      if ($row['gender'] == 'M') {
        echo 'Male';
      }
      if ($row['gender'] == 'F') {
        echo 'Female';
      }
      echo '</td></tr>';
    }
if (!empty($row['race'])) {
      echo '<tr><td class="label">Race:</td><td>' . $row['race'] . '</td></tr>';
    }

    echo '</table>'; //End of Table

    echo '<p>Would you like to <a href="editprofile.php?tutor_id=' . $_GET['tutor_id'] . '">edit your 

  } // End of check for a single row of user results
  else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }

  mysqli_close($dbc);
?>

 

Link to comment
Share on other sites

  $query = "SELECT tp.name, tp.nric, tp.gender, tp.race_id, r.race_name AS race" .
        "FROM tutor_profile AS tp " .
        "INNER JOIN race AS r USING (race_id) " .
   "WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
  
  $data = mysqli_query($dbc, $query);

 

try

 

  $query = "SELECT tp.name, tp.nric, tp.gender, tp.race_id, r.race_name AS race" .
        "FROM tutor_profile AS tp " .
        "INNER JOIN race AS r USING (race_id) " .
   "WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
  
  $data = mysqli_query($dbc, $query)
       or die(mysql_error());

   while ($rows = mysql_fetch_array($data) {
       $totalrows = COUNT(tutor_id);
   }

 

and change your if statement to:

 

  if (mysqli_num_rows($data) == 1) {

 

  if ($totalrows == 1) {

 

I had a similar problem earlier and that's what seemed to work for me =/.

Link to comment
Share on other sites

Myoga-, while your suggestion to add some error checking logic to find out why the query is failing is the correct course of action, posting 'fixed' and rewritten code that has nothing to do with the original code is not.

 

genzedu777 is using mysqli The only recommended change to the code would be to use -

 

  $data = mysqli_query($dbc, $query)
       or die(mysqli_error($dbc));

Link to comment
Share on other sites

Hi all,

 

After adding the...

 

$data = mysqli_query($dbc, $query)

or die(mysqli_error($dbc));

 

I had this error message...

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tutor_profile AS tp INNER JOIN race AS r USING (race_id) WHERE tp.tutor_id = 'Tr' at line 1

 

It puzzled me...I have been using this code for my other files, it works. The only difference between this file and the rest, is the newly added "WHERE tp.tutor_id = '" . $_GET['tutor_id'] . "'";

 

Can someone please help me, I am at my wits end! Thanks!!!

 

$query = "SELECT tp.name, tp.nric, tp.gender, tp.race_id, r.race_name AS race" .
        "FROM tutor_profile AS tp " .
        "INNER JOIN race AS r USING (race_id) " .
"WHERE tp.tutor_id = '" . $_GET['tutor_id'] . "'";

 

Link to comment
Share on other sites

Hi,

 

I realised if I were to use this SQL command, it works.

 

$query = "SELECT name, nric, gender, race_id FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

 

But it defeats the purpose, since the name of the race is kept in race.sql not in tutor_profile.sql, so it will end up reflecting an integer for race_id, and not the name, hence that is the reason I need to use INNER JOIN

 

Any suggestions? Thanks

 

Link to comment
Share on other sites

The query error is most likely being caused because you don't have any white-space right before the FROM in the query and it looks like -

 

SELECT tp.name, tp.nric, tp.gender, tp.race_id, r.race_name AS raceFROM tutor_profile AS tp INNER JOIN race AS r USING (race_id) WHERE tp.tutor_id = '" . $_GET['tutor_id'] . "'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.