Jump to content

Problems "while" looping


siwelis

Recommended Posts

$sql = mysql_query("SELECT * FROM shopping_products
ORDER BY product_id
");

$string1 = '<table>'.
'<tr><td>WAZZA!</td></tr>';

while($row = mysql_fetch_array($sql){
$string1 .= '<tr><td>THAT'S RIGHT!</td></tr>'.
'<tr><td>THAT'S RIGHT!</td></tr>';
}

$string1 .= </table>'


echo $string1;

 

All I get from this is "<table>

<tr><td>WAZZA!</td></tr>

</table>"

 

Why isn't the while putting the info into the string? Does anyone have some insight on another way to do this? I've also tried making it

$string1 .=  while($row = mysql_fetch_array($sql){
'<tr><td>THAT'S RIGHT!</td></tr>'.
'<tr><td>THAT'S RIGHT!</td></tr>';
}

 

and that doesn't seem to help either.

 

Link to comment
https://forums.phpfreaks.com/topic/55726-problems-while-looping/
Share on other sites

Syntax error here:

while($row = mysql_fetch_array($sql)) {
$string1 .= '<tr><td>THAT'S RIGHT!</td></tr>'.
'<tr><td>THAT'S RIGHT!</td></tr>';
}

//and here

$string1 .= '</table>';

For starts.

 

The second one wouldn't work as you cannot have the while inside a variable declaration.

 

Third, make sure there is data coming from the query, that shopping_products does contain some rows, because a query will return true and return no results.

 

I would suggest putting this at top for testing purposes

 

 

error_reporting(E_ALL);

 

As your code is not showing the syntax errors. It may help out.

I like to do it like this:

 

$sql = mysql_query("SELECT * FROM shopping_products
ORDER BY product_id");
$numrows = mysql_num_rows($sql);
if($numrows == 0){
  echo "There are no results available.";
} else {
  echo "<table><tr><td>WAZZA!</td></tr>"; // this is outside the loop
  while($row = mysql_fetch_array($sql)) {
    echo "<tr><td>THAT'S RIGHT!</td></tr>";
}
    echo "</table>"; // this is outside the loop
}

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.