Jump to content

Trying to display data from databse in a php table


BrainBoxMad

Recommended Posts

Hi everyone,

 

I am quite new to the world of PHP, and really need some help, as i am getting the following error message over and over again, and cant find a solution:

 

Parse error: syntax error, unexpected 'while' (T_WHILE), expecting ',' or ';' in E:\website\htdocs\test\test.php on line 27

 

Can anyone please give me some of their time and check through my code to maybe see if they can spot the problem area?

 

Thanks.

 

My Code is the following:

 

<?php

include 'config.php';

//Choose the database
$selected = mysql_select_db("store",$dbhandle)
  or die("Could not select store");
 
//execute the SQL query and return results
$result = mysql_query("SELECT product_id, product_name, product_type, price, description, stock, weight FROM products");

echo "<table border=\"1\" cellpadding=\"3\">\n";
echo "<thead>\n";
echo "<tr>\n";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Product Type</th>";
echo "<th>Price</th>";
echo "<th>Description</th>";
echo "<th>Stock</th>";
echo "<th>Weight</th>";
echo "</thead>\n";
echo "<tbody>\n"

//retrieve that data from the database, while loop due to having more than one result

while($row = mysql_fetch_array($result))

{
   echo "ID: ".$row{"product_id"}. "<br>";
   echo "Name: ".$row{"product_name"}. "<br>";
   echo "Product Type: ".$row{"product_type"}. "<br>";
   echo "Price: ".$row{"price"}. "<br>";
   echo "Description: ".$row{"description"}. "<br>";
   echo "Stock: ".$row{"stock"}. "<br>";
   echo "Weight: ".$row{"weight"}. "<br>";
   "<br>"; //display the results
}

?>

Edited by BrainBoxMad
Link to comment
Share on other sites

Yes, thank you L2c,

 

however, i am now getting the data displayed, but not correctly, as you can see below, any ideas why this may be happening?

 

Connected to MySQL
ID: 1
Name: ASUS 15" Ultrabook
Product Type: Laptop
Price: 400
Description: Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish
Stock: 15
Weight: 8

ID Name Product Type Price Description Stock Weight
Link to comment
Share on other sites

your actual data output doesn't contain any <tr><td>...</td>...</tr> tags, so it is just output as is. you would probably want to add those tags if you want the data to be displayed in the table. you will also want to close the table after all the output with a </table> tag.

Link to comment
Share on other sites

If you want to display in a table, I suggest you this code 

 

<php

..................................

?>

<html><head></head>

<body>

<?php

while($row = mysql_fetch_array($result))

{    ?>  <tr><td><?php  echo $row{"product_id"} ;?></td>
  

<td><? php  echo $row{"product_name"} ?></td></tr>
.........................................
....................................
...............................  
?>
</body>
</html>
Link to comment
Share on other sites

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.