Richard Stibbard's "MySQL/PDO Connection" - A mini-course

A series of mini-projects showing how to connect to a MySQL database using PDO (PHP Data Objects) in PHP.
English
How to connect to a MySQL database using PDO in PHP

Requirements

  • The course assumes you know how to create a MySQL database in phpMyAdmin but provides all source code necessary

Description

First launched in 2014, this simple "evergreen" series of short tutorials has proven popular ever since, and shows how to connect to a MySQL database using a PDO connection and display the database records in the browser.

Why use PDO?

There is a choice of only two ways to connect to a MySQL database, either mysqli, the 'i' standing for 'improved', or PDO. All the old mysql_* commands are long-since deprecatedand inherently a security risk. No mysql_* commands should be used any more, and they should be replaced in old code.

Both PDO and mysqli are equivalent in terms of security as both support prepared statements, meaning that user input is not entered straight into the SQL query but sent along a different channel where it is sanitised and safe to use. Both remove the risk of SQL injection inherent in using mysql_ commands.

Unlike mysqli, PDO supports named parameters and placeholder question marks; mysqli only supports placeholders. I will show how to work with both in this tutorial and you will see that named parameters are an easier and more convenient way of working with database fields easier.

Unlike mysqli, PDO supports many different database vendors – mysqli as we would gather from the name, only supports MySQL databases. If you did have to swap from one database type to another you would still have work to do as the SQL queries would be different, but at least it would be possible without many changes.

In summary, PDO is secure, flexible, and convenient and the best choice for all new projects and updates, so let's get started.

This tutorial assumes you know how to create a MySQL database in phpMyAdmin on your local machine, but to speed things up I provide an SQL file for you to import the database you need to do the course into phpMyAdmin.

Who this course is for:

  • Beginner web developer students learning PHP

Course content

1 section9 lectures34m total length
  • Introduction
    02:29
  • Lesson 1 - Connection
    05:19
  • Lesson 2 - Error catching
    02:52
  • Lesson 3 - Query method
    03:00
  • Lesson 4 - Fetch method
    03:42
  • Lesson 5 - FetchAll method
    03:18
  • Lesson 6 - Prepared statements with placeholders
    06:51
  • Lesson 7 - Prepared statements with named parameters
    02:58
  • Lesson 8 - Insert, update and delete records
    04:28

Instructor

Web Developer and Online Educator
Richard Stibbard
  • 4.8 Instructor Rating
  • 830 Reviews
  • 29,091 Students
  • 4 Courses

I have been working in web development since 2006, building custom database-driven websites from scratch, as well as customizing Moodle sites.  Before that, I was a lecturer at the University of Surrey, where I specialized in distance education through the online learning platform WebCT Vista (later Blackboard). 

I thus bring to my work in web development instruction a wealth of expertise not just in the technical aspects of the course, but also the educational side of online learning. My aim is to deliver courses of supreme clarity which are suitable for a range of learners, and which make learning a pleasure for all my students.