This document discusses interacting with MySQL databases from PHP using the Zend framework. It provides a basic 4 step process:
1. Include the Zend database library
2. Connect to the database by creating a configuration array and using Zend's factory method
3. Pass SQL statements to the database connection object and store the results
4. Loop through the result set to output or format the data
2. Business Assignments – let me know ahead of time if you are having trouble Grades are in Collab Would you prefer assignments to be given in Collab? Will give one assignment over break that will count for two (10% of grade)
9. LEFT JOINs SELECTd.country_name, d.debt, n.network FROMcountry_debtd LEFT JOINcountry_networkn ON (d.country_name = n.country_name) THIS WILL INCLUDE ALL FIELDS FROM THE LEFT TABLE (country_debt) EVEN IF THERE ARE NO MATCHES IN THE RIGHT TABLE
10. Flipping LEFT JOINs SELECTn.country_name, n.network, d.debt FROM country_networkn LEFT JOINcountry_debtd ON (d.country_name = n.country_name) THIS WILL INCLUDE ALL FIELDS FROM country_network EVEN IF THERE ARE NO MATCHES IN country_debt NOTE THAT COLUMN NAMES WERE CHANGED
11. Some WHERE Clauses country_name LIKE ‘China’ country_name LIKE ‘%orea%’ debt = 60.1 debt > 50 debt > 50 AND debt < 100 debt BETWEEN 50 AND 100 debt IS NOT NULL
12. VIEWS Finally, SQL lets you save the results of JOINs as virtual tables These behave just like real tables except that you cannot (usually) add data to them We will create views using phpMyAdmin’s “Create View” command which appears in the lower right of a view page
13. Overview Today we are going to look at how to interact with MySQL from within PHP Basic Pattern Include the database library Create a connection to the database Pass a SQL statement to the connection object and get array from the connection object Loop through arrays with the data
15. Step 1. Include the Library require(‘Zend/Db.php’); About include() and require() These allow you to put external file directly into your page Difference is that require throws an error is no file found About Zend A collection of libraries to build web applications PHP knows where to find it
16. Step 2. Connect to the Database Create an array with your credentials $config = array( 'host' => 'dbm2.itc.virginia.edu', 'username' => ’yourusername', 'password' => ‘yourpassword’, 'dbname' => ’yourdatabasename' );
17. Step2. Connect to the Database You may want to put your password in another file and include it. Create a new file db_creds.php Put in one line <?php $db_pwd = ‘password’; ?>, Add this line to your code // Defines my password $db_pwd require('db_creds.php');
18. Step 2. Connect to the Database Call Zend’s function to create a database connection Takes the $config array as an argument Returns a $db object Don’t worry about the weird syntax $db = Zend_Db::factory('Mysqli', $config);
19. Step 3. Pass SQL to $db $sql = “SELECT …” $rs = $db->FetchAll($sql); Notes The Zend Library is “object oriented” You only need to know the syntax Functions are attached to objects $db and $rs are variable for database and result set objects View $db and $rs using print_r
20. Step 4. Loop through $rs Loop through the result set and format column data as desired How to do this? HINT: It’s an array, so use the foreach() function
21. Exericses Create a table from the results Use CSS classes to add style Create links out of country names to Wikipedia