Page 1 of 1

[SOLVED] Formatting results into table format

Posted: Mon Feb 28, 2005 9:03 am
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

Posted: Mon Feb 28, 2005 9:06 am
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;

Posted: Mon Feb 28, 2005 9:09 am
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.

Posted: Mon Feb 28, 2005 9:11 am
by shab620
Hye

Tried both but still no joy,

No results are being displayed

any other suggestions?

Posted: Mon Feb 28, 2005 9:18 am
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???

Posted: Mon Feb 28, 2005 9:18 am
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.

Posted: Mon Feb 28, 2005 9:21 am
by shab620
Hye

The results are being displayed correctly now

Thanks for the help everyone

Shab