How to connect MySql database using PHP. PHP provides inbuilt functions to connect to MySql database using their extensions. In this post, I’ll explain how to connect MySql Database using PHP.
In PHP, you can connect MySql database using following extensions.
1) MySql (Worked only if you are using PHP Version 5.4 or less than that)
2) MySqli (MySql Improved)
3) PDO (PHP Data Objects)
Mysql API extension is deprecated from PHP 5.4 and now this extension is removed from the current version of PHP. So it’s better to learn how to connect MySql database using MySqli and PDO .
Advantage of using MySqli and PDO
1. MySqli and PDO both supports parameterized query which is helpful in preventing an application from SQL injection .
2. Both supports object-oriented interface.
3. Accessing database through MySqli and PDO is faster than previously used MySql API extension.
Difference Between MySqli and PDO
How to Connect MySql Database using PHP
To access MySql database in your code, you need to do following steps –
1. Open a connection.
2. Specify and Connect to the database.
3. Once connection is establish, query(Select/Insert/Update) the database.
4. Close the connection.
Connect MySql Database using PHP MySqli
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$server = 'localhost'; $user = 'root', $password = ''; $dbname = 'test'; // Creating connection object. $connection = new mysqli($server, $user, $password, $dbname); /* If connection is successful, connect_errno method returns 0 otherwise specific errno.*/ if($connection->connect_errno){ die('Could not connect'); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//Connection class class DB { protected $db_name = 'test'; protected $db_user = 'root'; protected $db_pass = ''; protected $db_host = 'localhost'; // Open a connect to the database public function connect() { $connect_db = new mysqli( $this->db_host, $this->db_user, $this->db_pass, $this->db_name ); if ( mysqli_connect_errno() ) { printf("Connection failed: %s\ ", mysqli_connect_error()); exit(); } return true; } } |
Read detail post here – How to connect MySql Database using PHP MySqli.
Connect MySql Database using PHP PDO
1 2 3 4 5 6 7 8 9 10 11 12 |
$username = 'root'; $password = ''; try { // Creating PDO object $DatabaseConn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password); } catch (Exception $e) { // If exception occurs echo $e->getMessage(); } |
To prevent an application from SQL injection try to use the prepared statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$username = "raj"; $password = "foo"; try { // Creating PDO object $DatabaseConn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password); // Parameterized query $query = $DatabaseConn->prepare('SELECT * FROM users WHERE username = :username and password = :password'); $query->execute(array('username' => $username, 'password' => $password)); while($row = $query->fetch()) { print_r($row); } } catch (Exception $e) { // If exception occurs echo $e->getMessage(); } |
Read detail post on connecting MySQL database using PDO.