PHP Connect to MySQL

PHP Connect to MySQL

Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. Curently, PHP 5 and later can work with a MySQL database using:

  • MySQLi extension (the "i" stands for improved)
  • PDO (PHP Data Objects)

MySQLi Versus PDO

You can make use of any of two for your database system. the following can enlighting you on when and where to use either one.

PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases.

So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.

Both are object-oriented, but MySQLi also offers a procedural API. Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.

For Linux and Windows: The MySQLi extension is automatically installed in most cases, when php5 mysql package is installed. For installation details, go to: http://php.net/manual/en/mysqli.installation.php

For installation details, go to: http://php.net/manual/en/pdo.installation.php

`

MySQL Examples in Both MySQLi and PDO

In the folowing examples we keep making samples of both mysqli both in the object- oriented packages and procedural method and also PDO

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)

`
  •       
                            <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password);
    
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>
                        

    Example (MySQLi Procedural)

    `
  •       
                            <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password);
    
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
    ?>
                        

    Example (PDO)

    `
  •       
                            <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
    ?>
                        

    Close the Connection

    A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

    MySQLi Object-Oriented:

  •       
                             $conn->close();
                        

    MySQLi Procedural:

  •       
                             mysqli_close($conn);
                        

    MySQLi Object-Oriented:

  •       
                            $conn = null;
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1