Pre-requisites:
- XAMPP: local database
PHP Functions:
The most frequently used function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows. Other ways of selecting the database is mysqli_fetch_assoc() selects the rows of the database as associative array.CODE:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'customers';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT `user_id`, `user_name`, `user_phone` FROM users';
$res = mysqli_query( $sql, $conn);
if(! $res) {
die('Could not get data: ' . mysql_error());
}
while($row = mysqli_fetch_assoc($res))
{
echo "USER ID :{$row['user_id']} ".
"USER NAME : {$row['user_name']} ".
"USER PHONE : {$row['user_phone']}".
"--------------------------------";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
EXPLANATION:
In the above code we have created four variables which stores the- Hostname of the server.
- Username.
- Password
- Name of the database.