Knowledge Posted January 12, 2011 Share Posted January 12, 2011 Hello everyone! I am trying to pull from mysql a row of first names in a while loop but I keep getting the error message Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line 52 The code is; <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $full_name = $_POST['firstname'] . ' ' . $_POST['lastname']; require_once ('swdb_connect.php'); $query = "INSERT INTO description(firstname, lastname) VALUES ('$first_name', '$last_name')"; $result = @mysql_query ($query); while($row = mysqli_fetch_array($result)) { echo $row['firstame'] . '<br />'; ?> I don't understand why I get the error message, I have over 20 names in mysql If anyone can help that would be great Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/ Share on other sites More sharing options...
Zurev Posted January 12, 2011 Share Posted January 12, 2011 Hello everyone! I am trying to pull from mysql a row of first names in a while loop but I keep getting the error message Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line 52 The code is; <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $full_name = $_POST['firstname'] . ' ' . $_POST['lastname']; require_once ('swdb_connect.php'); $query = "INSERT INTO description(firstname, lastname) VALUES ('$first_name', '$last_name')"; $result = @mysql_query ($query); while($row = mysqli_fetch_array($result)) { echo $row['firstame'] . '<br />'; ?> I don't understand why I get the error message, I have over 20 names in mysql If anyone can help that would be great You're using a mysqli fetch function with a mysql query, two different libraries. See MySQL vs MySQLi (Improved) Switch mysqli_fetch_array to mysql_fetch_array. Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/#findComment-1158588 Share on other sites More sharing options...
AbraCadaver Posted January 12, 2011 Share Posted January 12, 2011 Two problems: 1. You use mysql_query but mysqli_fetch_array. mysql and mysqli are not usable together. 2. You have executed an INSERT query which returns true or false (boolean). How can you fetch results from that? You fetch from a SELECT query. Also, get rid of the @ error suppression and do something like mysql_query($query) or die(mysql_error()); so you know what problems you have. Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/#findComment-1158589 Share on other sites More sharing options...
Knowledge Posted January 12, 2011 Author Share Posted January 12, 2011 Thank you for replying gentlemen. Both your replies fixed my error, small problem is I no longer get an error message, I just dont get a list of the names I have listed in my mysql database. Here is what I have updated it to. <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; require_once ('swdb_connect.php'); $query = "INSERT INTO description(firstname, lastname) VALUES ('$first_name', '$last_name')"; $result = mysql_query ($query); $query = "SELECT * FROM description"; $result = mysql_query ($query) or die('ERROR querying database.'); while($row = mysql_fetch_array($result)) { echo $row['firstame'] . '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/#findComment-1158600 Share on other sites More sharing options...
Rayhan Muktader Posted January 12, 2011 Share Posted January 12, 2011 Hey, Fix your spelling: echo $row['firstame'] 'firstname' Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/#findComment-1158615 Share on other sites More sharing options...
Knowledge Posted January 13, 2011 Author Share Posted January 13, 2011 Sir, you are correct. The script is now working. I want to thank you all for the help. Quote Link to comment https://forums.phpfreaks.com/topic/224239-while-loop-not-working/#findComment-1158714 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.