mectorfectorvector
mectorfectorvector

Reputation: 43

Retrieving MySQL values and printing them

The problem is, that when run, the script returns only the first row's user_email value. I want the script to compile a list, but this is not happening.

//Retreive database entries for emails
$fetch = mysql_query("SELECT user_email FROM users");
$rows = mysql_fetch_array($fetch);
//Store input in local variables
echo "<ul>";
while ($rows) {
    echo "<li>".$rows['user_email']."</li>";
}
echo "</ul>";

Upvotes: 0

Views: 36

Answers (2)

Jeremy Board
Jeremy Board

Reputation: 181

//Retreive database entries for emails
$fetch = mysql_query("SELECT user_email FROM users");
 //Store input in local variables 
echo "<ul>"; 
while ($rows = mysql_fetch_array($fetch) ) {
echo "<li>".$rows['user_email']."</li>";
}
echo "</ul>";

Upvotes: 1

Qirel
Qirel

Reputation: 26450

You should place your mysql_fetch_array as the while-condition, not outside.

while ($rows = mysql_fetch_array($fetch)) {
    echo "<li>".$rows['user_email']."</li>";
}

This will loop through all results from the database, rather than just repeating the first (as it's always returning true, but only for the first row).

Also, you should seriously consider converting to mysqli_* with prepared statements or PDO to prevent SQL-injection.

Upvotes: 3

Related Questions