Connect to a database, and select table with PHP

Here’s how to connect to a mysql database with php


<?php
$username = "username“;
$password = “password“;
$hostname = “database_hostname“; 

$dbh = mysql_connect($hostname, $username, $password)
 or die(”Unable to connect to MySQL”);

$selected = mysql_select_db(”database_name“,$dbh)
 or die(”Could not select database”);

// Selects the database
$result = mysql_query(”SELECT * FROM address”, $dbh);

// Starts a loop until EOF is reached

if(!$result) die(”Query Failed.”);
while($row = mysql_fetch_row($result)) {

// Shows column 1
echo $row[0], ” “;
echo $row[1], ” “;
echo $row[2], ” “;

echo “<br>”;

}

mysql_close($dbh);
?>

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment