Successfully reported this slideshow.

Mdst 3559-03-01-sql-php

417 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Mdst 3559-03-01-sql-php

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

×