PHP Connect to MySQL Main Tips
- Two main methods to work with the MySQL database: MySQLi extension and PDO (PHP Data Objects).
- Both methods have their advantages.
- This post demonstrates three different examples how to connect to a database: MySQLi (object-oriented), MySQLi (procedural) and PDO.
- Two main methods to work with the MySQL database: MySQLi extension and PDO (PHP Data Objects).
- Both methods have their advantages.
- This post demonstrates three different examples how to connect to a database: MySQLi (object-oriented), MySQLi (procedural) and PDO.
MySQLi Installation
MySQLi extension usually installs automatically with PHP5 MySQL package. This works on Linux and Windows operating systems.
For installation details, go to: http://php.net/manual/en/mysqli.installation.php
PDO Installation
For installation details, go to: http://php.net/manual/en/pdo.installation.php
Open a Connection to Database
Before starting to modify the MySQL database, we need to establish the connection with a server.
This example shows how to make a connection using MySQLi Object-Oriented method.
Example (MySQLi Object-Oriented)
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
-
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error)
- {
- die("Failed to connect: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
This example displays different MySQLi method.
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error)
- {
- die("Failed to connect: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
Example (MySQLi Procedural)
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
-
- // Create connection
- $conn = mysqli_connect($servername, $username, $password);
- // Check connection
- if (!$conn)
- {
- die("Failed to connect: " . mysqli_connect_error());
- }
- echo "Connected successfully";
- ?>
There is a database (myDatabase) specified in the PDO example below. This is because PDO requires a database for the connection. Examples above do not require a database name for connecting to the server (it will be needed when accessing the database itself).
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password);
- // Check connection
- if (!$conn)
- {
- die("Failed to connect: " . mysqli_connect_error());
- }
- echo "Connected successfully";
- ?>
Example (PDO)
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
- try
- {
- $conn = new PDO("mysql:host=$servername; dbname=myDatabase", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- echo "Connected successfully";
- }
- catch(PDOException $e)
- {
- echo "Failed to connect: " . $e->getMessage();
- }
- ?>
Note: One of the benefit of using PDO is the “try-catch” blocks. It can catch any errors in the “try” block and execute error handling code in “catch” block.
- <?php
- $servername = "localhost";
- $username = "name_of_the_user";
- $password = "users_password";
- try
- {
- $conn = new PDO("mysql:host=$servername; dbname=myDatabase", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- echo "Connected successfully";
- }
- catch(PDOException $e)
- {
- echo "Failed to connect: " . $e->getMessage();
- }
- ?>
Note: One of the benefit of using PDO is the “try-catch” blocks. It can catch any errors in the “try” block and execute error handling code in “catch” block.
Close the Connection
The connection closes automatically when the script stops running. If you want to close the connection earlier, use the following syntax:
Example (MySQLi Object-Oriented)
- $conn->close();
- $conn->close();
Example (MySQLi Procedural)
- mysqli_close($conn);
- mysqli_close($conn);
Example (PDO)
- $conn = null;
- $conn = null;
0 comments:
Post a Comment