+ All Categories
Home > Technology > Mdst 3559-03-01-sql-php

Mdst 3559-03-01-sql-php

Date post: 30-Jun-2015
Category:
Upload: rafael-alvarado
View: 301 times
Download: 0 times
Share this document with a friend
20
SQL + PHP MDST 3559: Dataesthetics Prof. Alvarado 03/01/2011
Transcript
Page 1: Mdst 3559-03-01-sql-php

SQL + PHP

MDST 3559: DataestheticsProf. Alvarado03/01/2011

Page 2: Mdst 3559-03-01-sql-php

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)

Page 3: Mdst 3559-03-01-sql-php

Review

• MySQL– Importing data – Creating tables and fields

• SQL– SELECT statements

Page 4: Mdst 3559-03-01-sql-php

SELECT

SELECT [DISTINCT] f1, f2, f3, …FROM t1, t2, …WHERE (some condition is true)ORDER BY (one or more fields)

Page 5: Mdst 3559-03-01-sql-php

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

Page 6: Mdst 3559-03-01-sql-php

Table Aliases

SELECT d.country_name, d.debt, n.networkFROM country_debt d, country_network n WHERE d.country_name = n.country_name

Page 7: Mdst 3559-03-01-sql-php

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

Page 8: Mdst 3559-03-01-sql-php

Alternate JOIN Syntax

SELECT d.country_name, d.debt, n.networkFROM country_debt dJOIN country_network n ON (d.country_name = n.country_name)

Page 9: Mdst 3559-03-01-sql-php

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

Page 10: Mdst 3559-03-01-sql-php

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

Page 11: Mdst 3559-03-01-sql-php

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

Page 12: Mdst 3559-03-01-sql-php

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

Page 13: Mdst 3559-03-01-sql-php

Getting Started

• Create subdirectory 03-01• Create file lesson.php

Page 14: Mdst 3559-03-01-sql-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

Page 15: Mdst 3559-03-01-sql-php

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'

);

Page 16: Mdst 3559-03-01-sql-php

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');

Page 17: Mdst 3559-03-01-sql-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);

Page 18: Mdst 3559-03-01-sql-php

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

Page 19: Mdst 3559-03-01-sql-php

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

Page 20: Mdst 3559-03-01-sql-php

Exericses

• Create a table from the results• Use CSS classes to add style• Create links out of country names to

Wikipedia


Recommended