Date post: | 30-Jun-2015 |
Category: |
Technology |
Upload: | rafael-alvarado |
View: | 301 times |
Download: | 0 times |
SQL + PHP
MDST 3559: DataestheticsProf. Alvarado03/01/2011
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)
Review
• MySQL– Importing data – Creating tables and fields
• SQL– SELECT statements
SELECT
SELECT [DISTINCT] f1, f2, f3, …FROM t1, t2, …WHERE (some condition is true)ORDER BY (one or more fields)
JOINs
• Combines two or more tables. E.g.:SELECT country_debt.country_name, country_debt.debt, country_network.networkFROM country_debt, country_networkWHERE country_debt.country_name = country_network.country_name
Table Aliases
SELECT d.country_name, d.debt, n.networkFROM country_debt d, country_network n WHERE d.country_name = n.country_name
Column Aliases
SELECT d.country_name as 'country', d.debt, n.networkFROM country_debt d, country_network n WHERE d.country_name = n.country_name
Alternate JOIN Syntax
SELECT d.country_name, d.debt, n.networkFROM country_debt dJOIN country_network n ON (d.country_name = n.country_name)
LEFT JOINs
SELECT d.country_name, d.debt, n.networkFROM country_debt dLEFT JOIN country_network n 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
Flipping LEFT JOINs
SELECT n.country_name, n.network, d.debt FROM country_network nLEFT JOIN country_debt dON (d.country_name = n.country_name)
THIS WILL INCLUDE ALL FIELDS FROM country_network EVEN IF THERE ARE NO
MATCHES IN country_debtNOTE THAT COLUMN NAMES WERE
CHANGED
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
Overview
• Today we are going to look at how to interact with MySQL from within PHP
• Basic Pattern1. Include the database library2. Create a connection to the database3. Pass a SQL statement to the connection object
and get array from the connection object4. Loop through arrays with the data
Getting Started
• Create subdirectory 03-01• Create file lesson.php
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
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'
);
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_pwdrequire('db_creds.php');
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);
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
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
Exericses
• Create a table from the results• Use CSS classes to add style• Create links out of country names to
Wikipedia