Creating database connection
PHP provides mysql_connect function to open a database connection. Handling database in PHP
connection mysql_connect(server,user,passwd,new_link,client_flag);
- server : Optional , The host name running database server. If not specified then default value is localhost:3306.
- user : Optional , The username accessing the database. If not specified then default is the name of the user that owns the server process.
- password : Optional, The password of the user accessing the database. If not specified then default is an empty password.
- new_link : Optional, If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.
- client_flags :Optional, A combination of the following constants −
MYSQL_CLIENT_SSL − Use SSL encryption
MYSQL_CLIENT_COMPRESS − Use compression protocol
MYSQL_CLIENT_IGNORE_SPACE − Allow space after function names
MYSQL_CLIENT_INTERACTIVE − Allow interactive timeout seconds of inactivity before closing the connection
Closing Database Connection
Its simplest function mysql_close PHP provides to close a database connection. This function takes connection resource returned by mysql_connect function. It returns TRUE on success or FALSE on failure. Handling database in PHP
bool mysql_close ( resource $link_identifier );
Example
<?php $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($conn); ?>
Insert Data into MySQL Database
Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query.
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = '"'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'INSERT INTO student '. '(std_name,std_address, class, join_date) '. 'VALUES ( "guest", "XYZ", "ty", NOW() )'; mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } echo "Entered data successfullyn"; mysql_close($conn); ?>
Example with HTML form :
<html> <head> <title>Add Record in MySQL Database</title> </head> <body> <?php if(isset($_POST['add'])) { $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if(! get_magic_quotes_gpc() ) { $std_name = addslashes ($_POST['std_name']); $std_address = addslashes ($_POST['std_address']); }else { $std_name = $_POST['std_name']; $std_address = $_POST['std_address']; } $std_class = $_POST['std_class']; $sql = "INSERT INTO student ". "(std_name,std_address, std_class, join_date) ". "VALUES('$std_name','$std_address',$std_salary, NOW())"; mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } echo "Entered data successfullyn"; mysql_close($conn); }else { ?> <form method = "post" action = "<?php $_PHP_SELF ?>"> <table width = "400" border = "0" cellspacing = "1" cellpadding = "2"> <tr> <td width = "100">Student Name</td> <td><input name = "std_name" type = "text" id = "std_name"></td> </tr> <tr> <td width = "100">student Address</td> <td><input name = "std_address" type = "text" id = "std_address"></td> </tr> <tr> <td width = "100">Student Salary</td> <td><input name = "std_salary" type = "text" id = "std_salary"></td> </tr> <tr> <td width = "100"> </td> <td> </td> </tr> <tr> <td width = "100"> </td> <td> <input name = "add" type = "submit" id = "add" value = "Add Student"> </td> </tr> </table> </form> <?php } ?> </body> </html>
Getting Data From MySQL Database
Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both.
Example
<?php $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT std_id, std_name, std_class FROM student'; mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "EMP ID :{$row['std_id']} <br> ". "EMP NAME : {$row['std_name']} <br> ". "EMP SALARY : {$row['std_class']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfullyn"; mysql_close($conn); ?> Handling database in PHP