[SOLVED] Formatting results into table format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

[SOLVED] Formatting results into table format

Post by shab620 »

Hye
I'm trying to display some results in a simple table but nothing is being diplayed.

The headings of the table are being diaplyed but no results are being displayed

I've quries the table using a simple print statement just to check that results are there and the results are being displayed.

the code I'm using is as follows

Code: Select all

<html>
<body>

<?
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="*********"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = "emp_order";
mysql_select_db($database);

echo "<table width="700" height="85"  cellpadding="0" border="1" cellspacing="0">"; 
echo "<tr><td> Order id</td><td></td><td> Item Description</td><td> Quantity</td><td> Comments</td><td> username</td>"; 
$result = mysql_query ("SELECT order_id, item_description, quantity, comments, user_name FROM emp_order",$connect);

while ($row = mysql_fetch_row($result))

&#123;

$id= $result&#1111;'order_id'];
$item_description= $result&#1111;'item_description'];
$quantity= $result&#1111;'quantity'];
$comments= $result&#1111;'comments'];
$user_name= $result&#1111;'user_name'];

echo "<tr><td>". $id . " </td><td> " .$item_description. "  </td><td>". $comments . "</td><td> " .$user_name. "  </td>"; 
 &#125; 

echo "</table>"; 

mysql_close($connect);
?> 
</table></body></HTML>
I have even tried to select the specific fields in the sql query but still no results

Can anyone help, I'm not an expert at PHP as you might tell :roll: :roll:

Or could you point to the direction of a simple table format result query display example

Shab
Last edited by shab620 on Mon Feb 28, 2005 9:22 am, edited 1 time in total.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

mysql_fetch_row() returns an array that contains numeric indices only. Your code will work with mysql_fetch_array() or mysql_fetch_assoc().
You also need to refer to the $row rather than to $result.

Code: Select all

while ($row = mysql_fetch_assoc($result))

&#123;

$id= $row&#1111;'order_id'];  // etc

 &#125;
Last edited by Bill H on Mon Feb 28, 2005 9:10 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

If you want a better reault (indexed by fieldname I would recommend you use mysql_fetch_assoc, although as Feyd has pointed out a couple of posts ago mysql_fetch_array would also work.

It would be useful if you also included some debug information to track errors such as db failures.
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye

Tried both but still no joy,

No results are being displayed

any other suggestions?
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye

Changed the code to $ROW and the results are being displayed

My problem now is that the columns are not lineing up correctly, for some reason an empty column for the second column is being inserted and pushing the columns up by one, leaving username(lastcolumn) as empty.

All the data from the table is being displayed.

Anyone have any ideas???
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Are you now checking $row['field'] rather than $result['field'].

If yes try placing some debug information in.

Code: Select all

$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
if (!$connect) &#123;
   die('Could not connect: ' . mysql_error());
&#125;

$table_name = "emp_order";
$db_selected = mysql_select_db($database, $connect);
if (!$db_selected) &#123;
   die ("Can't use $database : " . mysql_error());
&#125;
.
.
.
etc
Oops you beat me to it. DEbug information is still relevant.
Last edited by CoderGoblin on Mon Feb 28, 2005 9:22 am, edited 1 time in total.
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye

The results are being displayed correctly now

Thanks for the help everyone

Shab
Post Reply