MYSQLi CONNECT


PHP 5 and above recommends connecting to MySQL using the following methods:

·   MySQLi extension ("i" means improved)

·    PDO (PHP Data Objects)

In the early PHP release we used the MySQL extension. However, this extension was not recommended in 2012, because a set of functions of mysql has been obsolete.

Should I use MySQLi or PDO?


If you need a short answer, "Which one is used to which one?"

MySQLi and PDO have their own advantages:

PDO is used in 12 different databases, and MySQLi is only for MySQL databases.

So, if your project needs to switch between multiple databases, it is recommended to use PDO, so you only need to modify the connection string and department query. With MySQLi, if you have a different database, you need to rewrite all the generations

Code, including queries.

Both are object oriented, but MySQLi also provides an API interface.

Both support prepared statements. Prepared statements prevent SQL injection and are very important for the security of web projects.

MySQLi and PDO connect to MySQL instance

In this and the following chapters, we will use any of the following three ways to demonstrate PHP operation MySQL:

· MySQLi (Object Oriented)

· MySQLi (process oriented)

· PDO

Connect to MySQL 


Grammar

Mysql_connect (servername, username, password);


Parameter       Description
Servername
Optional. Specifies the server to connect to. The default is "localhost:3306".

UsernameOptional. Specifies the username used for login. The default is the name of the user who owns the server process.
Password    Optional. Specifies the password used for login. The default is "".
 
Before we can access the MySQL database, we need to connect to the database server first:

Instance (MySQLi - object oriented)


 
 $servername = "localhost";
 $username = "username";
 $password = "password";
 
 // Create a connection
 $conn = new mysqli($servername, $username, $password);
 
 / / Check the connection
 If ($conn->connect_error) {
     Die("Connection failed: " . $conn->connect_error);
 }
 Echo "Connection succeeded";
 ?>
Note that $connect_error was added in PHP 5.2.9 and 5.3.0 in the above object-oriented instance. If you need to be compatible with an earlier version, please replace it with the following code:
// Detect connection 
if (mysqli_connect_error()) { 
    die("database connection failed: " . mysqli_connect_error()); 
}

Instance (MySQLi - Process Oriented)
 
 $servername = "localhost";
 $username = "username";
 $password = "password";
 
 // Create a connection
 $conn = mysqli_connect($servername, $username, $password);
 
 / / Check the connection
 If (!$conn) {
     Die("Connection failed: " . mysqli_connect_error());
 }
 Echo "Connection succeeded";
 ?>
Instance (PDO)
 $servername = "localhost";
 $username = "username";
 $password = "password";
 
 Try {
     $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
     Echo "Connection succeeded";
 }
 Catch(PDOException $e)
 {
     Echo $e->getMessage();
 }
 
Note : We have specified the database (myDB) in the above PDO instance. PDO needs to set the database name during the connection process. If not specified, an exception will be thrown

Close the connection


The connection is automatically closed when the script is executed. You can also use the following code to close the connection:

Instance (MySQLi - object oriented)

 $conn->close();
 Instance (MySQLi - Process Oriented)
Mysqli_close($conn);
 Instance (PDO)
$conn = null;

Instance

In the following example, we store the database connection in a variable ($con). If the connection fails, the "die" part is executed; if the connection is successful, the connection is closed after the program is executed.

 
 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $con = mysqli_connect("localhost","root","root","test");
 If (!$con)
 {
     Die('Could not connect: ' . mysqli_error());
 }
 Else{
     Echo "Connected to the database successfully";
 }
 // some code
 Mysqli_close($con);
 
 
The above code means to connect to a server named localhost, the username is root, the password is root, the test database is used; if the connection fails, the code inside the die is executed.

Program running result:

Successful connection to the database

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :