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
saumya
Forum Contributor
Posts: 193 Joined: Sun Jan 30, 2005 10:21 pm
Post
by saumya » Tue Aug 16, 2005 12:32 am
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
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
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Tue Aug 16, 2005 12:46 am
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 » Tue Aug 16, 2005 1:10 am
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 » Tue Aug 16, 2005 1:17 am
just a min.Does this makes a multidimensional array? or just an arry?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 16, 2005 7:17 am
his example only pulls the first row of result data. It's a single level array.
gurjit
Forum Contributor
Posts: 314 Joined: Thu May 15, 2003 11:53 am
Location: UK
Post
by gurjit » Tue Aug 16, 2005 11:47 am
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
Post
by biznickman » Tue Aug 16, 2005 11:58 am
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
saumya
Forum Contributor
Posts: 193 Joined: Sun Jan 30, 2005 10:21 pm
Post
by saumya » Wed Aug 17, 2005 2:24 am
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>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Aug 17, 2005 2:34 am
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 » Wed Aug 17, 2005 2:40 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Aug 17, 2005 2:47 am
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 » Wed Aug 17, 2005 4:34 am
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