Jump to content

Archived

This topic is now archived and is closed to further replies.

OriginalSunny

Getting all info from database

Recommended Posts

Hi,
just a quick question. I have created a table with headings for each column in my database. I now want to display everything from my database in php. I have got the following code:


$connect = connect_to_db("cnn.inc");
$query = "select * from products";
$result = mysql_query($query,$connect)
or die("sql_del: ".mysql_error($connect));

I now want to display all the items. The only way i can think is to do some sort of an array to go through each entry in the database. I don'd know exaclty how to do this properly. I can display each item seperately using:

$prod = mysql_result($result1,0,0);
echo "$prod";

but as you know this is a silly and time consuming way to do it for each entry. As i am fairly new to php i don't know how to do it properly.
Thanks.

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$connect = connect_to_db("cnn.inc");
$query = "select * from products";
$result = mysql_query($query,$connect)
or die("sql_del: ".mysql_error($connect));
while ($row = mysql_fetch_array($result)) {
echo($row['field1'] $row['fieald2'].'<br>');
}[/quote]


That will loops thru and instead of field1 field2 change them to when ever the columns are called in your table.

That should do the job mate :)

Share this post


Link to post
Share on other sites
Thanks. It works. But the trouble i am having now is that all the headings for my table are being output and then the values in the columns are being output below the table. I want them in the following format:

OrderID 200 233
Surname Smith Terry

...

The code i am using is:

echo "<table cellspacing='20'>";

echo "<tr>OrderID</tr><br>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo($row['orderID'].'<br>');
echo"</td>";
}
echo "<tr>Surname</tr><br>";
while($row1 = mysql_fetch_array($result))
{
echo "<td>";
echo($row['surname'].'<br>');
echo"</td>";
}

I cant see what i have done wrong here. In addition to this only the values for orderID are output and the values stored for surname are not ouput unless i get rid of the array displaying the orderID's. Your help would be appreciated.
Thanks.

Share this post


Link to post
Share on other sites

×

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.