how to display a table data from mysql in html?

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
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

how to display a table data from mysql in html?

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you so much.
let me try and i will get back.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

just a min.Does this makes a multidimensional array? or just an arry?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

his example only pulls the first row of result data. It's a single level array.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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>
biznickman
Forum Newbie
Posts: 13
Joined: Tue Aug 16, 2005 10:32 am

Alternative

Post 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]
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

changing mysql_fetch_arry to mysql_fetch_array may help.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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
Post Reply