Page 1 of 1

how to display a table data from mysql in html?

Posted: Tue Aug 16, 2005 12:32 am
by saumya
hi everybody,
I am new in php-mysql.Can you people guide me for a simple question.
Actually i have a database, and now in my html page i want to show all the datas in a particular table to the user in an html or php page.I know this can be done and the dats are also formatted in a table format to the user but could not know how to do that.When i pass the querry

Code: Select all

SELECT * FROM mytable
it shows the table data in the console window.But if i pass the same query in my php page it just returns some id not all the datas.Please help me out understanding the method.
thanks

Posted: Tue Aug 16, 2005 12:46 am
by s.dot
You'll need to store the data in an array, and pick out the parts you need

Code: Select all

<?
$SQL = "SELECT * FROM tbl_data";
$RESULT = mysql_query($SQL);
$ARRAY = mysql_fetch_assoc($RESULT);
?>

<html>
<head>
<title>Some Page</title>
</head>
<body>
<table>
  <tr>
    <td><? echo $ARRAY['field1']; ?></td>
  </tr>
  <tr>
    <td><? echo $ARRAY['field2']; ?></td>
  </tr>
</table>
</body>
</html>

Posted: Tue Aug 16, 2005 1:10 am
by saumya
thank you so much.
let me try and i will get back.

Posted: Tue Aug 16, 2005 1:17 am
by saumya
just a min.Does this makes a multidimensional array? or just an arry?

Posted: Tue Aug 16, 2005 7:17 am
by feyd
his example only pulls the first row of result data. It's a single level array.

Posted: Tue Aug 16, 2005 11:47 am
by gurjit
try this for multi records:

Code: Select all

<?php

$SQL = "SELECT id,name FROM tbl_data"; 
$mysql_result = mysql_query($SQL,$conn);
?> 

<html> 
<head> 
<title>Some Page</title> 
</head> 
<body> 
<table> 
<?php 
 while ($row = mysql_fetch_array($mysql_result))
{
		
$id = $row["id"];
$name = $row["name"];
							
					?>
  <tr> 
    <td><? echo $id . " " . $name; ?></td> 
  </tr> 
<?php } ?>
</table> 
</body> 
</html>

Alternative

Posted: Tue Aug 16, 2005 11:58 am
by biznickman
An alternative would be as follows

Code: Select all

<?php
$sql = "SELECT * FROM myTable";
$result = mysql_query( $sql ) or die(mysql_error());
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<table width='600' align='center'>
<?php
$i=0;
while( $assoc = mysql_fetch_assoc( $result ) ){
if( $i == 0 ){
?>
<tr>
  <?php foreach( $assoc as $k => $v ){ ?>
  <td><?=$k?></td>
  <?php } ?>
</tr>
<?php  } ?>

<tr>
    <?php foreach( $assoc as $k => $v ){ ?>
    <td><?=$v?><td>
    <?php } ?>
</tr>
<?php $i++; } ?>
</table>
</body>
</html>

feyd | hey look, we have

Code: Select all

tags. [/color]

Posted: Wed Aug 17, 2005 2:24 am
by saumya
i have tried this but no good.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>Filled Data</title>
 </head>
 <body>
   <table>
   <?php
    mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
    mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
    $sql="SELECT * FROM daily";
    $mysql_result=mysql_query($sql);
   //.................................................
   //.................................................
     while($row=mysql_fetch_arry($mysql_result)){
     print"<tr><td>{$row['slno']}</td></tr>";
     print"<tr><td>{$row['dt']}</td></tr>";
     print"<tr><td>{$row['item']}</td></tr>";
     print"<tr><td>{$row['price']}</td></tr>";
     }
   ?>
   </table>
 </body>
</html>

Posted: Wed Aug 17, 2005 2:34 am
by feyd
changing mysql_fetch_arry to mysql_fetch_array may help.

Posted: Wed Aug 17, 2005 2:40 am
by saumya
thanks.thanks a lot.
that helped.It now outputs.But no table format.How can i make that in aa table format?

well i am sorry.I made it.
thanks.
But still does confused about the workings of the getting data out of databse.I mean the mechanism.Can you please help me understand what it is doing.It will be so nice of you.

Posted: Wed Aug 17, 2005 2:47 am
by feyd
this is a quick and dirty, good starting point...

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Filled Data</title>
</head>
<body>
   <table>
   <?php
    mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
    mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
    $sql="SELECT * FROM daily";
    $mysql_result=mysql_query($sql);
   //.................................................
   //.................................................
     $first = true;
     while($row=mysql_fetch_assoc($mysql_result))
     {
       if($first) echo '<tr><th>'.implode('</th><th>',array_keys($row)).'</th></tr>';
       $first = false;
       echo '<tr><td>'.implode('</td><td>',array_values($row)).'</td></tr>';
     }
   ?>
   </table>
</body>
</html>
obviously, this will perform now beutifying as it's just trying to display the data you pulled, nothing more.

Posted: Wed Aug 17, 2005 4:34 am
by saumya
well could not got you though,but thanks.I just tried modifying the code and it worked.The code is

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>Filled Data</title>
 </head>
 <body>
  <center>
   <table border="2">
   <tr>
     <td width="100">Sl.No</td>
     <td width="100">Date</td>
     <td width="100">Item Name</td>
     <td width="100">Price</td>
   </tr>
   <?php
    mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
    mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
    $sql="SELECT * FROM daily";
    $mysql_result=mysql_query($sql);
   //.................................................
   //.................................................
     while($row=mysql_fetch_array($mysql_result)){
     print"<tr><td>{$row['slno']}</td>";
     print"<td>{$row['dt']}</td>";
     print"<td>{$row['item']}</td>";
     print"<td>{$row['price']}</td></tr>";
     }
   ?>
   </table>
 </center>
 </body>
</html>
could not understand the underlying logic but it works.Can u help me understand what exactly it is doing?
thanks