Showing PHP data in HTML Tables

A

Anonymous

Guest
OK I have spent the last hour searching google for some free information on how to show my mySQL data in Tables but no luck, Also tried playing with code but no luck.

Code for getting data
-------------------------------------
<?php
$db = mysql_connect("localhost","sillyfoo_admin","5177ym4n");
mysql_select_db("sillyfoo_database1",$db);
//replace the above values with your actual database values

$res=mysql_query("SELECT * FROM News");
if(mysql_num_rows($res)==0) echo "There is no data in the table";
else

for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo "ID : $row[Title] Name : $row[Description]";
}

?>

HTML Tables to show data
-------------------------------------------
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="63%" id="AutoNumber1">
<tr>
<td width="100%">Title Goes here</td>
</tr>
<tr>
<td width="100%">Description goes here</td>
</tr>
</table>

:help:
 
..and? Are you getting errors or something? We need to know what those errors are.
 
I dont know how to put the code so that it shows the data retrived from mysql in the right place in table!
 
Put in loop like this :

$yourdata .= "<tr><td>ID : $row['Title']</td></tr>
<tr><td> Name : $row['Description']</td></tr>" ;

then in html table :

<table>
<?php $yourdata; ?>
</table>

try it
 
viralupadhyaya said:
Put in loop like this :

$yourdata .= "<tr><td>ID : $row['Title']</td></tr>
<tr><td> Name : $row['Description']</td></tr>" ;

then in html table :

<table>
<?php $yourdata; ?>
</table>

try it
it should be

Code:
 $yourdata .= "<tr><td>ID : {$row['Title']}</td></tr>
                     <tr><td> Name : {$row['Description']}</td></tr>" ;

then  in html table :

<table>
<?php $yourdata; ?>
</table>

IF you want to reference to an assoc array in a string... you need to use { and }
 
I am trying to use below code I get error


Parse error: parse error, unexpected $ in /home/sillyfoo/public_html/test/tables.php on line 22

Code:
<?php 
$db = mysql_connect("localhost","sillyfoo_admin","5177ym4n"); 
mysql_select_db("sillyfoo_database1",$db) or die( "Unable to select database");
//replace the above values with your actual database values 

$res=mysql_query("SELECT * FROM News");
if(mysql_num_rows($res)==0) echo "There is no data in the table";
else

for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res) or die(mysql_error());

$yourdata .= "<tr><td>ID : {$row['Title']}</td></tr> 
                     <tr><td> Name : {$row['Description']}</td></tr>" ; 

?>

<table> 
<?php $yourdata; ?> 
</table>
 
please use proper parenthesesis for if ,then else structures first
 
Back
Top