MySql support is stopped from PHP on next versions. So we have to use MySqli or PDO. 
 Let know about PDO(PHP Data Object) first Connect to Database using PDO is simpler than MySQL, we require some details similar as MySql that are

DSN : Mostly we use for MySQL is "mysql", we can use "pqsql" also for "Postgre SQL" and sqlite for "SQL Lite.

HOST : The hostname on which the database server resides. 

PORT : If port changed by Administrator we need to give that too. Default port number where the database server is listening is 3306. 

DBNAME : The name of the database. 

CHARSET : The character set. (Example: UTF-8) 

USER : Username of the database can accessed 

PASS : Password of the User

In this Manditory are

DSN 
HOST 
DBNAME 
USERNAME 
PASSWORD

$hostname = "localhost";
$dbname = "database_name";
$dsn = 'mysql:host='.$hostname.';dbname='.$dbname; //You can also give details
directly here

$username = 'username';
$password = 'password';
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',); //Not Needed



$con = new PDO($dsn, $username, $password, $options); //Options Not needed

(or)
$con = new PDO("mysql:localhot;dbname=database_name", "username", "password");
$con->exec("set names utf8");
(or)
$con = new PDO("mysql:localhot;dbname=database_name;port:3306", "username", "password");

We can Handle Errors of Database by
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); //No Error Reporting
$con
->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );//Warnings
$con
->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Exceptions
 
Follow First Method for Easy use.

0 comments:

Post a Comment

 
Top