Jump to content

SOLVED Display List Of Data From MySql Table


RobberBaron
 Share

Recommended Posts

I'm trying to output a list from a table in MySql using PHP. The way I'm doing it used to work on other things such as memberlists, but this seems to want to output every occurance an infinite number of times. In other words, when the page loads it will have a list showing hundreds of the first row from the database and will continue trying to load it. This is the code I'm using:

While($info=mysql_fetch_array(mysql_query('SELECT * FROM projects'))) {	Echo('<li><a href="project.php?id='.$info['ID'].'" title="'.$info['title'].'">'.$info['title'].'</a>|<a onclick="$.post(\'delete.php\', {\'webid\': '.$info['ID'].'});">X</a>|</li>');};

Is it a problem with the while?

Edited by RobberBaron
Link to comment
Share on other sites

You are executing the query over and over again, so the pointer keeps getting reset. You are supposed to create a result resource, then use mysql_fetch_array() to iterate over the resulting variable.

$result = mysql_query('SELECT * FROM projects');while($info=mysql_fetch_array($result)) {	echo('<li><a href="project.php?id='.$info['ID'].'" title="'.$info['title'].'">'.$info['title'].'</a>|<a onclick="$.post(\'delete.php\', {\'webid\': '.$info['ID'].'});">X</a>|</li>');};

Link to comment
Share on other sites

You are executing the query over and over again, so the pointer keeps getting reset. You are supposed to create a result resource, then use mysql_fetch_array() to iterate over the resulting variable.
$result = mysql_query('SELECT * FROM projects');while($info=mysql_fetch_array($result)) {	echo('<li><a href="project.php?id='.$info['ID'].'" title="'.$info['title'].'">'.$info['title'].'</a>|<a onclick="$.post(\'delete.php\', {\'webid\': '.$info['ID'].'});">X</a>|</li>');};

Ahah, makes sense. Thanks :)
Link to comment
Share on other sites

The semicolon represents an empty statement:

$result = mysql_query('SELECT * FROM projects');while($info=mysql_fetch_array($result)) {	echo('<li><a href="project.php?id='.$info['ID'].'" title="'.$info['title'].'">'.$info['title'].'</a>|<a onclick="$.post(\'delete.php\', {\'webid\': '.$info['ID'].'});">X</a>|</li>');}NULL; // a line doing nothing

As such, it is not necessary, but not syntactically incorrect.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...