Working with Database (MySQL)
Introduction
The MySQL delivers a very fast, multi-threaded, multi-user, and robust SQL (Structured Query Language) database server. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in a database.
Description
resource mysql_connect ([ string $server [, string $username [, string $password [, bool $new_link [, int $client_flags ]]]]] )
Parameters
server – The MySQL server.
username – The MySQL username
password – The MySQL password
new_link
client_flags
Return Value – Returns a MySQL link identifier on success, or FALSE on failure.
Consider that we have a database in our localhost. The username be ‘mysql_user’ and password be ‘mysql_password’, the connection script is as follows
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect:'.mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
mysql connect using hostname
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
mysql connect to localhost using ip
<?php
//we connect to localhost at port 3307
$link= mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
mysql_select_db – Select a MySQL database
Description – bool mysql_select_db ( string $database_name [, resource $link_identifier ] )
<?php
// Make a MySQL Connection
mysql_connect("localhost", "example", "example123") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());
?>




