People keep on mentioning that I should be using PDO in my PHP when dealing with MySQL, I have never heard of this before.
What is PDO? How is it used and what are t
PDO stands for PHP Data Objects. This is an interface that allows PHP scripts to query a database via SQL queries.
PDO is an extension that is added to PHP so that its various functionalities are available in the language. It constitutes an abstraction interface of the database, that is to say that all of its functions can be used to execute SQL queries whatever the DBMS. In other words, if the web application is based on the MySQL DBMS, we can migrate to the PostgreSQL DBMS without modifying the source code (some minor modifications are required).
The abstraction of the database is a strong point compared to the old methods of accessing them. Moreover, it constitutes the ultimate advantage of the PDO, without being the only one.
The proper functioning of PDO depends on the availability of the database driver. This must be supported to be able to query the desired DBMS.
To declare the SBGD MySQL driver for example, go to the php.ini file and add the following line (if it is not already declared): extension = php_pdo_mysql.dll Note that the following line must also appear: extension = php_pdo.dll This step is necessary for PHP versions lower than 5.3. Using the PDO (Connection to a database) object To simplify the explanation, I will use an example:
Suppose that the database server is the local server (localhost) and the database (MySQL) that we want to query is called "mabase". To be able to query this database, we will use the username "user" and the password "1234". Of course, the user "user" has just enough privileges to query the database via PHP scripts.
It is now assumed that in the "mabase" database, a table named "users" has been created, the structure of which is as follows:
CREATE TABLE `utilisateurs` (
`id` int(10) unsigned NOT NULL auto_increment,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`nom` varchar(40) NOT NULL,
`prenom` varchar(40) NOT NULL,
`login` varchar(40) NOT NULL,
`pass` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
);
I guess you are comfortable with SQL language. Otherwise, just run this code through the PHPMyAdmin tool on your server.
Now that all the settings for our database are there. We will see how to query it using PHP via the PDO object. Database Connection String (PDO Instance) The Connection String is the textual representation of the database connection information installed on a server. Declaring the connection string in this case is like creating a PDO instance like this: