It means that the submitted form data will go to `add.php`. To add data into database, we need an html form.įorm action on `add.html` is `add.php`. SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) // Setting Error Mode as Exception In below code, the database host name is `localhost` where `username=root` and `password=root`. This file is included in all PHP pages where database connection is necessary. This code connects to the MySQL database. Now, we will create a config.php file which contains database connection code. Then, we will create a new table in database ‘test’. Here is a step-by-step guide on creating a CRUD system using PHP & MySQL with PDO:įirst of all, we will create a new MySQL database. In this article, we will be creating the same system but using PDO extension driver to access database. That article/tutorial uses MySQLi extension driver for accessing the database. In the past, I had written an article on creating a Simple CRUD System using PHP & MySQL. Hence, preventing SQL injection attack to the system/application. The other benefit is that prepared statements are quoted automatically by the driver. The major benefit of prepared statements is that you just need to prepare a query once and then it can be run multiple times with the same or different parameters. PDO also supports prepared statements and stored procedures.
The exceptions are logged in a log file and a user friendly error message is displayed on screen. PDO functions can be written using try/catch block. The other benefit of PDO is error handling feature using exceptions.
PDO extension supports different databases like MS SQL, MySQL, Oracle, PostgreSQL, SQLite, etc. You don’t need to change your query function if you switch database server for your project. The best one is that it’s cross-database compatible. PDO is a PHP extension that provides an interface for accessing databases in PHP.
#Mysql php viewer how to#
This article shows how to create a CRUD (Create, Read, Update, Delete) application in PHP & MySQL using PHP Data Objects (PDO).