jose Posted April 30, 2010 Share Posted April 30, 2010 I have a problems with this script: I get this warning message: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C. I would appreciate any suggestions about how to solve it, manuals, source of information, since this message is very common. Thanks a lot in advance. J <?php require_once('connectvars.php'); // Generate the navigation menu if (isset($_COOKIE['username'])) { echo '❤ <a href="viewprofile.php">View Profile</a><br />'; echo '❤ <a href="editprofile.php">Edit Profile</a><br />'; echo '❤ <a href="logout.php">Log Out (' . $_COOKIE['username'] . ')</a>'; } else { echo '❤ <a href="login.php">Log In</a><br />'; echo '❤ <a href="signup.php">Sign Up</a>'; } // Connect to the database $dbc = mysqli_connect('localhost', '****', '*****', '*****') or die ('Error connecting to MySQL server'); // Retrieve the user data from MySQL $query = "SELECT user_id, first_name FROM user WHERE first_name IS NOT NULL ORDER BY join_date DESC LIMIT 5"; $result = mysqli_query($dbc, $query); // Loop through the array of user data, formatting it as HTML echo '<h4>Latest members:</h4>'; echo '<table>'; while ($row = mysqli_fetch_array($row)) { if (isset($_SESSION['user_id'])) { echo '<td><a href="viewprofile.php?user_id=' . $row['user_id'] . '">' . $row['first_name'] . '</a></td></tr>'; } else { echo '<td>' . $row['first_name'] . '</td></tr>'; } } echo '</table>'; mysqli_close($dbc); ?> Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2010 Share Posted April 30, 2010 Yes, it is one of the most common mysql related errors. It generally means that your query failed. However, in your case it is because you used the wrong variable name in the mysqli_fetch_array() function call. Did you look at the line of code producing the error and attempt to see what was wrong with the parameter you were passing it? Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/#findComment-1051079 Share on other sites More sharing options...
OOP Posted April 30, 2010 Share Posted April 30, 2010 I guess you meant to do the following: while ($row = mysqli_fetch_array($result)) { instead of this while ($row = mysqli_fetch_array($row)) { $result should be passed to the function mysqli_fetch_array instead of the $row Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/#findComment-1051082 Share on other sites More sharing options...
jose Posted April 30, 2010 Author Share Posted April 30, 2010 I tried this option, row for result. I get a similar message: boolean given in instead of null given in Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/#findComment-1051093 Share on other sites More sharing options...
andalib14 Posted April 30, 2010 Share Posted April 30, 2010 try this : $result = mysqli_query($query); //instead of $result = mysqli_query($dbc, $query); //and then $row=mysql_fetch_array($result); Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/#findComment-1051114 Share on other sites More sharing options...
jose Posted April 30, 2010 Author Share Posted April 30, 2010 Thank you guys for your help. The eventual solution is this: // Retrieve the user data from MySQL $query = "SELECT user_id, first_name FROM user WHERE first_name IS NOT NULL ORDER BY join_date DESC LIMIT 5"; $result = mysqli_query($dbc, $query); // Loop through the array of user data, formatting it as HTML echo '<h4>Latest members:</h4>'; echo '<table>'; while ($row_cnt = $result->num); { The old script is // Retrieve the user data from MySQL $query = "SELECT user_id, first_name FROM user WHERE first_name IS NOT NULL ORDER BY join_date DESC LIMIT 5"; $result = mysqli_query($dbc, $query); // Loop through the array of user data, formatting it as HTML echo '<h4>Latest members:</h4>'; echo '<table>'; while ($row = mysqli_fetch_array($row)) { Quote Link to comment https://forums.phpfreaks.com/topic/200285-warning-mysqli_fetch_array-expects-parameter-1-to-be-mysqli_result-null-give/#findComment-1051209 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.