Connect PHP to MySQL
What is MySQL?
MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP.
Structured Query Language (SQL). The data in a MySQL database are stored in tables that consist of columns and rows.
MySQL is a database system that runs on a server. MySQL is ideal for both small and large applications. MySQL is a very fast, reliable, and easy-to-use database system. It uses standard SQL. MySQL compiles on a number of platforms.
How we can connect PHP to MySQL?
PHP 5 and later can work with a MySQL database using:
- MySQLi extension (the ‘i’ is abbreviation for improved)
- PDO (PHP Data Objects)
Which one should we use MySQLi or PDO?
Both MySQLi and PDO have their recompenses:
- PDO will work with 12 different database systems, whereas MySQLi will only work with MySQL databases.
- So, if you have to shift your project to use alternative database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the complete code — queries included.
- Both are object-oriented, but MySQLi also offers a procedural API.
In short, you can choose whichever you want if you want to stick to MySQL otherwise you should go with PDO.
Connection to MySQL using MySQLi
PHP provides mysql_connect() function to open a database connection.
This function takes a single parameter, which is a connection returned by the mysql_connect() function.
You can disconnect from the MySQL database anytime using another PHP function mysql_close().
There is also a procedural approach of MySQLi to establish a connection to MySQL database from a PHP script.
It can be done in two ways:
Connection to MySQL using PDO
Please Login to comment...