October 2012

Types of connection

There are two types of connection with database in php. They are
  1. Non-Persistent connection : Connection is closed at end of script (end of page).
    mysql_connect(server,user,pwd,newlink,clientflag);

  2. Persistent connection : Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable).
    mysql_pconnect(server,user,pwd,newlink,clientflag);

Note : Persistent connections don't give you any functionality that wasn't possible with their non-persistent brothers.
Note : mysql_close(connection) // function closes a non-persistent MySQL connection.

If persistent connections don't have any added functionality, what are they good for?

Database Connection Mysql

Steps of using MySql db from Php.

  1. Create a database connection.
  2. Select a database.
  3. Perform a SQL query.
  4. Perform your operations on SQL query results.
  5. Close database connection. 
1. Create a database connection.
Php uses following funtions for making a databse connection .
mysql_connec(server,user,pwd,newlink,clientflag) // Non persistance connection
mysql_pconnec(server,user,pwd,newlink,clientflag) // Persistance connection.

Parameter Description
server Optional. Hostname of server. Default value is "localhost:3306"
user Optional. Username on the database. Default value is the name of the user that owns the server process
pwd Optional. Username on the database. Default is ""
newlink Optional. Reuse database connection created by previous call to mysql_connect
clientflag Optional. Can be 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

2. Select a database.
Php use following function for selecting database.
mysql_select_db(database,connection)
   database -- Specifies the database to select.
   connection -- (Optiona)l. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

3. Perform a SQL query.
Php have following function to executes a query on a MySQL database.
mysql_query(query,connection)
   query -- SQL query to execute (eg. SELECT query, UPDATE query...etc);
   connection -- (Optional) MySQL connection name. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
This function returns the query handle for SELECT queries, TRUE/FALSE for other queries, or FALSE on failure.
4. Close database connection. 
Php uses following function for closing a connection.
mysql_close(connection) // function closes a non-persistent MySQL connection.

Sample Code :
<?php
$con = mysql_connect("server","username","password");
if (!$con)   {
  die('Unable to connect: ' . mysql_error());  
}else{
mysql_select_db('database_name');
}
$sql = "SELECT * FROM Customer";
mysql_query($sql,$con);
// do some more precessing ..
mysql_close($con);
?>

Database Connection


  • PHP provides built-in database connection class/functios for a wide range of databases such as MySQL, PostgreSQL, Oracle, Berkeley DB, Informix, mSQL, Lotus Notes.
  • PHP also provides database connectivity for specific databases which involves PHP configuration steps.

Please refer following url for detail database connectivity for different databases
http://www.php.net/manual/en/

Newer Posts Older Posts

Related Posts Plugin for WordPress, Blogger...
Powered by Blogger.