Connect to MySQL Database and display first 5 results using PHP

Date Submitted Mon. Feb. 20th, 2006 11:22 AM
Revision 2 of 2
Beginner elbartoli
Tags connect | display | mysql | PHP
Comments 1 comments
Connect to MySQL db & display result from Table query


$username = "your_username";
$password = "your_password";
$db = "database_name";

//for anyone with a local server the $host can be set to "localhost" or ""
$host = "host_address";

$db_connection = mysql_connect($host, $username, $password) or die("Could not connect");

$MySQLSelectedDB = mysql_select_db($db, $db_connection) or die("Could not set the Database");

$res=mysql_query("SELECT * FROM table_name LIMIT 5");

//check that there is something in the table to display
if (mysql_num_rows($res)==0) {
echo "There is no data in the table";

        } else {

//extract the data and print it out with a simple echo for each field
for($i=0; $i<mysql_num_rows($res); $i++) {


echo "Field 1 : $row[field_1] Field 2 : $row[field_2] Field 3 : $row[field_3] </BR>";


//close connection



Bartley Sharkey


Comments A few problems/issues
Mon. Feb. 20th, 2006 11:07 AM    Helper mercutio