MySql - PDO Tutorial

Update mysql code to PDO tutorial.
Php Version required to use this PDO code is PHP 5.1 or higher

Reference source

Connection Code

mysql connection code may look something like this

  • $db=mysql_connect ("localhost", "database_name", "password") or die ('I cannot connect to the database because: ' . mysql_error());
    if (!$db) {die('Could not connect: ' . mysql_error()); }

PDO Connection code to connect to MySql database

  • $dsn = 'mysql:host=localhost;
    dbname = your_database;charset=utf8mb4';
    $username = 'username';
    $password = 'password';

    $db = new PDO($dsn, $username, $password);
Save this as connect_pdo.php in your includes folder.

Insert Code

mysql insert code may look something like this

  • $tbl_name='table_name';
    $sql="INSERT INTO $tbl_name(first_name, last_name, email)VALUES('$first_name', '$last_name', '$email')"; $result=mysql_query($sql);

PDO Insert code to insert data into MySql database

  • <?php
    // require the PDO connection code

    // assign the table name you will use

    // Run the query using prepared statement by binding the values and executing

    $query = "INSERT INTO $tbl_name(first_name, last_name, email)
    VALUES(:first_name, :last_name, :email)";
    $statement = $db->prepare($query);
    $statement ->bindValue(':first_name', $first_name);
    $statement ->bindValue(':last_name', $last_name);
    $statement ->bindValue(':email', $email);

Select Code

mysql select code may look something like this

  • $query1= "SELECT * FROM `table_name`";
    $res1 = mysql_query($query1);

PDO Select the data you choose to select

PDO Select code to select all data from MySql table_name

  • <?php
    // Display all from enquiry table
    $queryAll = 'SELECT * FROM table_name ORDER BY id';
    $statement = $db->prepare($queryAll);
    $statement->bindValue(':id', $id);
    $statement->bindValue(':first_name', $first_name);
    $statement->bindValue(':last_name', $last_name);
    $statement->bindValue(':email', $email);
    // You can use a timestamp field in database to record date of form submission
    $statement->bindValue(':enquiry_date', $enquiry_date);
    $results = $statement->fetchAll();

PDO Display the result set

PDO Select code using fetchAll to display selected data from MySql table_name

Using a simple html table to display this data but have not included all of the code for the html table

  • <?php foreach ($results as $result) : ?>
    <td><?php echo $result['id']; ?></td>
    <td><?php echo $result['first_name']; ?></td>
    <td><?php echo $result['last_name']; ?></td>
    <td><?php echo $result['email']; ?></td>
    <?php endforeach; ?>

Our Services

We supply modern forms using Php-MySql and ReCaptcha for any web site.

We supply responsive web site templates that will display correctly on all modern web enabled devices.

Complete our Enquiry Form on the Contact Us page for more information.

Enquiry Form

Recent Feedback

Code rating, 10/10, excellent service, your help in supplying our web form needs has been nothing short of excellent. The ready to place code arrived quickly, and we were able to install it on our own website with little effort

Experience, 10/10 the coding is execellent, thank you.

A very enjoyable business experience