genzedu777 Posted January 7, 2011 Share Posted January 7, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/ Share on other sites More sharing options...
Myoga- Posted January 7, 2011 Share Posted January 7, 2011 $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 =/. Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156181 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2011 Share Posted January 7, 2011 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)); Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156187 Share on other sites More sharing options...
genzedu777 Posted January 8, 2011 Author Share Posted January 8, 2011 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'] . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156523 Share on other sites More sharing options...
genzedu777 Posted January 8, 2011 Author Share Posted January 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156530 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2011 Share Posted January 8, 2011 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'] . "' Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156534 Share on other sites More sharing options...
genzedu777 Posted January 8, 2011 Author Share Posted January 8, 2011 Hi PFMaBiSmAd, Thank you so much. It is indeed the 'space' issue. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/223668-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boo/#findComment-1156669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.