Forum Moderators: coopster

Message Too Old, No Replies

Problems with formatting query output

         

Awful newbie

12:05 pm on May 30, 2007 (gmt 0)

10+ Year Member



I want my MySQL-output nicly formatted in an html table. But the code get distorted, I just cant see whats wrong. Anyone with better googles? The code:

<?php

include 'connection.inc';

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM my_database WHERE category='10' ORDER BY field1-name ASC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<table border='0' cellspacing='2' cellpadding='2'>
<tr>
<th><h3>Value1</h3></th>
<th><h3>Value2</h3></th>
<th><h3>Value3</h3></th>
<th><h3>Value4</h3></th>
<th><h3>Value5</h3></th>
</tr>;

$i=0;
while ($i < $num) {

<tr>
<td><b><? echo $field1-name;?></b></td>
<td><b><? echo $field2-name;?></b></td>
<td><b><? echo $field3-name;?></b></td>
<td><b><? echo $field4-name;?></b></td>
<td><b><? echo $field5-name;?></b></td>
</tr>

<?
$i++;
}

echo "</table>";
?>

eelixduppy

1:27 pm on May 30, 2007 (gmt 0)



Try something like this:
 
#make sure this is parsed as php and not displayed as regular text!
#you might want to change it to connection.inc.php to be sure
include 'connection.inc';
#
#localhost needs to be a string - added quotes
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM my_database WHERE category='10' ORDER BY field1-name ASC";
$result = mysql_query($query);
#
echo "<table border='0' cellspacing='2' cellpadding='2'>
<tr>
<th><h3>Value1</h3></th>
<th><h3>Value2</h3></th>
<th><h3>Value3</h3></th>
<th><h3>Value4</h3></th>
<th><h3>Value5</h3></th>
</tr>";#added a quote at the end here
#
while ($row = [url=http://www.php.net/mysql-fetch-assoc]mysql_fetch_assoc[/url]($result)) {
?>
<tr>
<td><b><? echo $row[$field1-name];?></b></td>
<td><b><? echo $row[$field2-name];?></b></td>
<td><b><? echo $row[$field3-name];?></b></td>
<td><b><? echo $row[$field4-name];?></b></td>
<td><b><? echo $row[$field5-name];?></b></td>
</tr>
<?
}
#
mysql_close();
echo "</table>";

Try that to see what you get. Take note of all the comments in there; there were many errors.

If you have further problems, post the results, and please give the errors that the script is producing, as well.

Good luck! :)

IndiaMaster

3:34 am on May 31, 2007 (gmt 0)

10+ Year Member



As you are using a while loop using a loop control variable $i so you may also use

echo mysql_result($result,$i,"field1_name");

instead of

$row[$field1-name];