Page 1 of 1

create a form from database contents

Posted: Sun Jan 22, 2012 9:29 pm
by jerry0258
I am attempting to create a test in which the questions are contained in a database. I am retrieving the results of the query into an indexed array, but when I try to print them to the screen I get put into an infinite loop. My code is posted below:

include "dbconnect.php";
$SQLstring = "SELECT questiontext FROM test";
$QueryResult = "mysql_query($SQLstring, $con)";
echo "<form action=' ' method='post'>";
echo "<table width='100%' border = '1'>\n";

while(($Row = mysql_fetch_row($QueryResult)) !== FALSE)
{
echo "<tr><td>{$Row[0]}</td></tr>";

}

echo "</table>\n";
echo "</form>";

It seems to connect to the database but when looping through the array does not come to an end (it should pull 10 results from the database). Thank you for any help anyone can provide.

Re: create a form from database contents

Posted: Sun Jan 22, 2012 10:36 pm
by califdon
jerry0258 wrote:

Code: Select all

include "dbconnect.php";
$SQLstring = "SELECT questiontext FROM test";
$QueryResult = "mysql_query($SQLstring, $con)";
echo "<form action=' ' method='post'>";
echo "<table width='100%' border = '1'>";

while(($Row = mysql_fetch_row($QueryResult)) !== FALSE)
	{
	echo "<tr><td>{$Row[0]}</td></tr>";
	
	}

echo "</table>\n";
echo "</form>";
It seems to connect to the database but when looping through the array does not come to an end (it should pull 10 results from the database). Thank you for any help anyone can provide.
Remove the quotes from the $QueryResult line.

While you're at it, remove the "!==FALSE" from the While line.

Code: Select all

include "dbconnect.php";
$SQLstring = "SELECT questiontext FROM test";
$QueryResult = mysql_query($SQLstring, $con);
echo "<form action=' ' method='post'>";
echo "<table width='100%' border = '1'>\n";

while($Row = mysql_fetch_row($QueryResult)) 
	{
	echo "<tr><td>{$Row[0]}</td></tr>";
	
	}

echo "</table>\n";
echo "</form>";

Re: create a form from database contents

Posted: Mon Jan 23, 2012 1:11 pm
by jerry0258
Thank you so much! I had looked and looked at it until I had convinced myself that it was perfectly correct. I appreciate your help!