MySQL PDO Tutorial - Introduction
44.2 هزار بار بازدید -
10 سال پیش
-
Enrol on the course at
Enrol on the course at https://www.webinaction.co.uk/p/mysql... to download the Working Files.
This is a short series of tutorials on how to connect to a MySQL database using a PDO connection and display the database records in the browser.
Why should we use PDO?
Well first there is a choice of only two ways to connect – either mysqli, the ‘i’ standing for ‘improved’, or PDO, which stands for ‘PHP data objects’.
The old mysql_* commands are deprecated and inherently a security risk and should no longer be used. If possible 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. So 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.
Before you begin, you will find the code for this project at http://webinaction.co.uk/coding-tutor...
In the zip file you will find the ‘testdb.sql’. In phpMyAdmin click on ‘Import’ and import this file and the database you need, ‘testdb’, will be created.
Once you've done that, let's get started...
This is a short series of tutorials on how to connect to a MySQL database using a PDO connection and display the database records in the browser.
Why should we use PDO?
Well first there is a choice of only two ways to connect – either mysqli, the ‘i’ standing for ‘improved’, or PDO, which stands for ‘PHP data objects’.
The old mysql_* commands are deprecated and inherently a security risk and should no longer be used. If possible 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. So 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.
Before you begin, you will find the code for this project at http://webinaction.co.uk/coding-tutor...
In the zip file you will find the ‘testdb.sql’. In phpMyAdmin click on ‘Import’ and import this file and the database you need, ‘testdb’, will be created.
Once you've done that, let's get started...
10 سال پیش
در تاریخ 1393/05/24 منتشر شده
است.
44,247
بـار بازدید شده