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!
I'm not sure if you're trying to do something special, but if you're not, the code you posted seems to me to be a very awkward way to display all the records in a table. The simplest way is simply:
mysql_connect('myhostname','mydbname','mypassword') or die(mysql_error());
mysql_select_db('mydbname') or die(mysql_error());
$sql="SELECT * FROM mytablename";
$resource=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($resource)) {
extract($row);
foreach($row as $fld=>$value) {
echo "$fld : $value<br />";
}
echo "<br />";
}
You don't need to create another array. $row is an array that is built into the mysql_fetch_* function. By using mysql_fetch_assoc, instead of one of the other variations, and using extract(), you will get pairs of field names and values for each row.
mysql_connect( 'myhostname','mydbname','mypassword' ) or die(mysql_error());
mysql_select_db( 'mydbname' ) or die(mysql_error());
$query = "SELECT * FROM mytablename";
$resource = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_object($resource) )
{
echo $row->name. ":". $row->description. "<br />";
}
Of course change columns name to your own.
Your code works IF you know the column names, of course. What I showed is a generic method that doesn't require that you know the column names, thus it can be used in all cases, merely substituting the table name.
nishmgopal wrote:Thanks alot for your help guys! I managed to extract all my data, but now how can I display this data in a html table? do i have to define each row?
mysql_connect( 'myhostname','mydbname','mypassword' ) or die(mysql_error());
mysql_select_db( 'mydbname' ) or die(mysql_error());
$query = "SELECT * FROM mytablename";
$resource = mysql_query( $sql ) or die(mysql_error());
while( $row = mysql_fetch_object($resource) )
{
echo $row->name. ":". $row->description. "<br />";
}
Of course change columns name to your own.
Your code works IF you know the column names, of course. What I showed is a generic method that doesn't require that you know the column names, thus it can be used in all cases, merely substituting the table name.
Yeah you right.. I guess you're the smart boy here