This tutorial will demonstrate how to connect your PHP script to a MySQL database. PHP provides a set of functions which makes your task of connecting to a database easier than you’d imagine. Initially, we will need the following set of functions to connect to a database.
mysql_connect($host,$username,$password): Connects to the host.
mysql_select_db($database_name): Connects to the specified database
mysql_query($query): Executes the specified sql query. It returns the query handler which can be used to extract data from the query result using mysql_fetch_array() function.
In the following example, we have connected to a database and listed all the entries fro
|
1 2 3 4 5 6 7 8 9 |
<?php $con = mysql_connect('localhost','mycoding','123') or die(mysql_error()); echo 'Connected to database.<br>'; mysql_select_db('login',$con)or die(mysql_error()); $result = mysql_query("select * from info")or die(mysql_error()); while($data = mysql_fetch_array($result)){ echo "Username: ".$data['username'].'<br>'; } ?> |
Output:
