Thursday, July 30, 2009

How can you do a simple output of a mysql table in php?

I have a user that wants to see the contents of a table. Is there a command that just dumps the table on the screen? I have a phpmyadmin account, but I don't want to release that password as I have other databases on the server. Any ideas?

How can you do a simple output of a mysql table in php?
The most simple way to do this is to run your SQL query and output the value of the array you put the results in:





%26lt;?php


$con = mysql_connect("localhost","peter","abc12...


if (!$con)


{


die('Could not connect: ' . mysql_error());


}





mysql_select_db("my_db", $con);





$result = mysql_query("SELECT * FROM person");





print_r($result);





mysql_close($con);


?%26gt;





However this doesn't format it results very well and is usually used for debugging. The following code formats the data in a HTML table. However this will require a little more adaption.





%26lt;?php


$con = mysql_connect("localhost","peter","abc12...


if (!$con)


{


die('Could not connect: ' . mysql_error());


}





mysql_select_db("my_db", $con);





$result = mysql_query("SELECT * FROM person");





while($row = mysql_fetch_array($result))


{


echo $row['FirstName'] . " " . $row['LastName'];


echo "
";


}





mysql_close($con);


?%26gt;


No comments:

Post a Comment