Jump to content

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
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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