How to Turn PHP Into MySQL Statements
MySQL is a database platform that allows a website to store data, such as the contents of a blog, photo gallery or other forms of information. However, MySQL does not interact directly with a web page. Instead, you need an intermediary language to extract the database information and send it to the web browser in the form of HTML. PHP is the most common language for this purpose, and it includes many built-in functions for creating MySQL statements.
Instructions
-
-
1
Identify the exact MySQL query you need for your database statement. Typical MySQL queries include the SELECT parameter, which simply pulls data out of the database. This is necessary when visitors to a website need to see the contents of the database.
-
2
Open the PHP file for the web page that will need the MySQL statement. For example, if you want to display blog contents to a visitor, open the PHP file for the page that needs to extract and display the database information.
-
-
3
Create a PHP variable that is not used elsewhere in your PHP file and assign it to the "mysql_query" PHP function. Enter the MySQL that you earlier identified. For example, if you wanted to extract information using a SELECT MySQL statement, your PHP command might look like this:
$query=mysql_query('SELECT * FROM DATABASE_TABLE');
-
4
Create another new PHP variable that will interact with the results of the query. The query result itself does not contain data that you can manipulate. Instead, you must parse it with a function such as "mysql_fetch_assoc" which will create an array for each record in the queries results. This syntax might appear as:
while ($array=mysql_fetch_assoc($query)) {
//$array will store associative array indexes that match your MySQL table fields
}
Other similar functions are "mysql_result" and "mysql_fetch_array" which all create PHP structures to handle the results of your MySQL statement.
-
1
Tips & Warnings
The performance of your PHP statements will improve by efficiently coding the MySQL portion of those queries. For example, combine multiple MySQL queries into one statement if possible so your PHP runs as few times as possible.