Jump to content

While Loop not working


Knowledge

Recommended Posts

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  ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
}

?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.