Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP PDO

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 481
    Comment on it

    Welcome to FindNerd.

    Today we are going to discuss PHP PDO. It is not a new term in PHP but many of us want to work in old patterns. Most common extensions are MySql and MySqli. If you are using PHP then you should be familiar with these terms. Database management is most common part in web development. Without security your data will be  unsecure. To manage the database operations you need to use most secure extension that is PDO. PDO stands for PHP Data Objects. It provides us the ability to use the different database platforms such as mysql, sqlLite, IBM and many more.

    If you want to check available database drivers then use this function.

    $arr_drivers = PDO::getAvailableDrivers();

     

    Very first step is the connection building. You need database driver of your choice. By creating the instance of the PDO class you can establish connection with database. Please have look.

    try {
    $db_user = "root";
    $db_pass =  "";
    $dbh = new PDO('mysql:host=localhost;dbname=findnerd', $db_user, $db_pass);
    } catch(Exception $e){
        echo $e->getMessage();
    }


    In above we made connection with sql

    You can also make connection with other database as well using their drivers. You are going to share the example for sqlLite
     

    $dbh = new PDO("sqlite:my/database/path/sqlLite.db");

    For error handling you need to use the try/catch block. There are three different error modes.

    A) PDO::ERRMODE_SILENT : This default mode does not show error and you need to check the error.

    B) PDO::ERRMODE_WARNING : It shows the warning and continue the execution.

    C) PDO::ERRMODE_EXCEPTION : It shows exceptions and gives you the different ways for handling.

    You can set the mode like this

    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    After making the connection you can write the database query in prepare statement. Prepare statements usually executes multiple times and reduce the chance of sqlInjection.
     

    try {
    $db_user = "root";
    $db_pass =  "";
    $dbh = new PDO('mysql:host=localhost;dbname=findnerd', $db_user, $db_pass);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $data = array('messy', 'dark bill', 'rt');
     
    $sth = $dbh->("INSERT INTO schole (name, addr, city) values (?, ?, ?);
    $sth->execute($data);
    }
    catch(Exception $e){
        echo $e->getMessage();
    }

    In above example we are trying to insert the data in table. We are using unnamed placeholder in prepare statement. With the help of execute function we are inserting the data.

     

    You can simply close the connection by using below statement

    $dbh = null;

    In next blog we will discuss the fetching process in PDO.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: