Jump to content


Photo

Getting all info from database


  • Please log in to reply
2 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 24 March 2006 - 11:16 PM

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.

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 24 March 2006 - 11:24 PM

[!--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 :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 March 2006 - 01:39 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users