The Very Basics of Using PHP scripts to access MySQL databases

 

Q: What is PHP? What is a PHP file composed of? What is the PHP scripting block in a PHP file?

 

A: Read Introduction to PHPand PHP Syntaxfrom the very simple PHP tutorial by W3Schools.

 

 

Q: How do I use PHP programming constructs such as comments, variables, if statements, for loops, arrays, and functions in PHP scripting blocks? How do I output text strings and/or store text strings in variables in PHP?

 

A: Read PHP Syntax, PHP Operators, PHP Conditional Statements,PHP Looping, and PHP Functionsfrom the very simple PHP tutorial by W3Schools. The official PHP website provides a much more comprehensive coverage of PHP language. You should read at least the section on arrays there.

 

 

 

Q: After a user browsed a web page, entered information into a form in the web page, and submitted the information (by the POST method or the GET method) to be processed by a PHP script file, how does the PHP script retrieve the information entered by the user?

 

A: The PHP script can use the $_REQUEST autoglobal to access form data sent by the GET method, the POST method and COOKIE data. Alternatively, the PHP script can use the $_POST autoglobal to access form data sent by the POST method, and the $_GET autoglobal to access form data sent by GET method. Read (i) PHP Forms from the very simple PHP tutorial by W3Schools, and also (ii) Dealing with Formsfrom the official PHP site.

 

 

Q: How does a PHP script (i) connect to MySQL server and login into a MySQL account (ii) select a database, (iii) execute a query,and (iv) print resulting records?

 

A#1: You can use the related MySQL functions in PHP to do the job. To understand the very basics of how a PHP scripts the PHP script communicates with MySQL. Study the PHP script used in Lab#7, and read the following descriptions of the very basic functions needed:

 

Here are some functions useful in retrieving the column information:

 

You donít need to use them, but here are some additional functions used in Example 1 in the MySQL functions page from the official PHP website.

 

A#2: You can also use the new improved MySQL functions in PHP to do the job. See MySQL Improved Extension from the official PHP website.