Created
March 7, 2012 15:51
-
-
Save smonteverdi/1993918 to your computer and use it in GitHub Desktop.
PHP: Connect to Database mysql way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$username = "your_name"; | |
$password = "your_password"; | |
$hostname = "localhost"; | |
//connection to the database | |
$dbhandle = mysql_connect($hostname, $username, $password) | |
or die("Unable to connect to MySQL"); | |
echo "Connected to MySQL<br>"; | |
//select a database to work with | |
$selected = mysql_select_db("examples",$dbhandle) | |
or die("Could not select examples"); | |
//execute the SQL query and return records | |
$result = mysql_query("SELECT id, model,year FROM cars"); | |
//fetch tha data from the database | |
while ($row = mysql_fetch_array($result)) { | |
echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results | |
$row{'year'}."<br>"; | |
} | |
//close the connection | |
mysql_close($dbhandle); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment