SQL queries in PHP - Why we need MySQL databases and SQL queries
Now that your PHP scripts run correctly, you might need to store some of the information that you encounter throughout the execution of your PHP scripts (the same applies for scripts developped in other web programming languages).
This can of course be achieved by storing the data within text files located on your web server, using the PHP functions that we have just introduced in our tutorial about file manipulation in PHP. The only problem with that method is that you do not have access to any of the pre-built functions, security tools and management solutions that a database system (such as MySQL) and its associated database scripting language (such as SQL) can offer you.
This is the reason why we are turning to web databases, which are basically arrays in which you will be able to store information about your users. Database systems will allow you to take advantage of very useful functions and easily perform, for instance, specific searches throughout the tables of your databases (without having to program these functions yourself, that is). Also, it provides a secure environment and database server connection protocol.
Materially speaking, databases are also here to help you preserve the integrity of your data, and are thus physically separated from the servers on which your applications are running. A database scripting language (such as SQL) will enable the connection between your PHP scripts and your databases.
In the following, we choose to introduce MySQL databases (using the SQL scripting language); the reasons for this choice have been given in our introductory tutorial about PHP scripts.
SQL queries in PHP - Different ways to run SQL queries
There are different ways to query your MySQL databases using SQL queries:
- you can run SQL queries directly from a command prompt in PHPMyAdmin or in any other MySQL database manager (we strongly recommend that you use phpmyadmin as your MySQL database manager; upon reading our tutorials about SQL with MySQL, you will be able to install and learn phpmyadmin on your own. Indeed, this software is very intuitive to use).
- you can insert SQL queries within the scripts you develop, and in particular within your PHP scripts. For this purpose, certain functions exist in PHP in order to allow you to run such SQL queries.
SQL queries in PHP - SQL query from a PHP script
In order to run SQL queries from your PHP script, you have to use certain PHP functions which will allow you to:
- Connect to a MySQL database server
- Run SQL queries on the MySQL database server in order to create/delete databases and tables, connect to databases, search database entries, insert/modify/delete dabase entries, etc ...
- Close the connection to the MySQL database server.
MySQL databases will be a usefool tool in order to store information gathered during the execution of your PHP scripts; the MySQL database system will provide you with a safe environment and a scripting language called SQL which is necessary to query your databases, either from a prompt command in a database management software (such as PHPMyAdmin) or directly from your PHP scripts.
Next tutorial: PHP regular expressions
Previous tutorial: Handling files in PHP
Back to computer forums
