SlideShare ist ein Scribd-Unternehmen logo
1 von 20
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) ORDERBY (one or more fields)
JOINs Combines two or more tables.  E.g.: SELECTcountry_debt.country_name, country_debt.debt, country_network.network FROMcountry_debt, country_network WHEREcountry_debt.country_name = country_network.country_name
Table Aliases SELECTd.country_name, d.debt, n.network FROMcountry_debtd, country_networkn WHEREd.country_name= n.country_name
Column Aliases SELECTd.country_nameas 'country', d.debt, n.network FROMcountry_debtd, country_networkn WHEREd.country_name= n.country_name
Alternate JOIN Syntax SELECTd.country_name, d.debt, n.network FROMcountry_debtd JOINcountry_networkn ON (d.country_name = n.country_name)
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
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
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
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
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
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_pwd require('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

Weitere ähnliche Inhalte

Was ist angesagt?

Presentation dual inversion-index
Presentation dual inversion-indexPresentation dual inversion-index
Presentation dual inversion-indexmahi_uta
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2ADARSH BHATT
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardJAX London
 
JSON Array Indexes in MySQL
JSON Array Indexes in MySQLJSON Array Indexes in MySQL
JSON Array Indexes in MySQLNorvald Ryeng
 
Scrap your query boilerplate with specql
Scrap your query boilerplate with specqlScrap your query boilerplate with specql
Scrap your query boilerplate with specqlTatu Tarvainen
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)MongoSF
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07Hassen Poreya
 
How to separate the f2 e and sde in web development for_taobao
How to separate the f2 e and sde in web development for_taobaoHow to separate the f2 e and sde in web development for_taobao
How to separate the f2 e and sde in web development for_taobaotaobao.com
 

Was ist angesagt? (10)

Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Presentation dual inversion-index
Presentation dual inversion-indexPresentation dual inversion-index
Presentation dual inversion-index
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
 
JSON Array Indexes in MySQL
JSON Array Indexes in MySQLJSON Array Indexes in MySQL
JSON Array Indexes in MySQL
 
Scrap your query boilerplate with specql
Scrap your query boilerplate with specqlScrap your query boilerplate with specql
Scrap your query boilerplate with specql
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07
 
How to separate the f2 e and sde in web development for_taobao
How to separate the f2 e and sde in web development for_taobaoHow to separate the f2 e and sde in web development for_taobao
How to separate the f2 e and sde in web development for_taobao
 
Sequel
SequelSequel
Sequel
 

Andere mochten auch

Mdst 3559-04-05-networks-and-graphs
Mdst 3559-04-05-networks-and-graphsMdst 3559-04-05-networks-and-graphs
Mdst 3559-04-05-networks-and-graphsRafael Alvarado
 
UVA MDST 3073 Texts and Models-2012-09-11
UVA MDST 3073 Texts and Models-2012-09-11UVA MDST 3073 Texts and Models-2012-09-11
UVA MDST 3073 Texts and Models-2012-09-11Rafael Alvarado
 
Mdst 3559-01-27-data-journalism-studio
Mdst 3559-01-27-data-journalism-studioMdst 3559-01-27-data-journalism-studio
Mdst 3559-01-27-data-journalism-studioRafael Alvarado
 
Mdst 3559-03-03-sql-php-2
Mdst 3559-03-03-sql-php-2Mdst 3559-03-03-sql-php-2
Mdst 3559-03-03-sql-php-2Rafael Alvarado
 
Mdst 3559-03-15-web-apps
Mdst 3559-03-15-web-appsMdst 3559-03-15-web-apps
Mdst 3559-03-15-web-appsRafael Alvarado
 
MDST 3703 F10 Seminar 12
MDST 3703 F10 Seminar 12MDST 3703 F10 Seminar 12
MDST 3703 F10 Seminar 12Rafael Alvarado
 
Mdst3703 2013-09-24-hypertext
Mdst3703 2013-09-24-hypertextMdst3703 2013-09-24-hypertext
Mdst3703 2013-09-24-hypertextRafael Alvarado
 
Mdst3559 2011-05-03-final-day
Mdst3559 2011-05-03-final-dayMdst3559 2011-05-03-final-day
Mdst3559 2011-05-03-final-dayRafael Alvarado
 

Andere mochten auch (20)

Asteroïden
AsteroïdenAsteroïden
Asteroïden
 
MDST 3703 F10 Seminar 1
MDST 3703 F10 Seminar 1MDST 3703 F10 Seminar 1
MDST 3703 F10 Seminar 1
 
Mdst 3559-04-05-networks-and-graphs
Mdst 3559-04-05-networks-and-graphsMdst 3559-04-05-networks-and-graphs
Mdst 3559-04-05-networks-and-graphs
 
UVA MDST 3073 Texts and Models-2012-09-11
UVA MDST 3073 Texts and Models-2012-09-11UVA MDST 3073 Texts and Models-2012-09-11
UVA MDST 3073 Texts and Models-2012-09-11
 
MDST 3703 F10 Studio 11
MDST 3703 F10 Studio 11MDST 3703 F10 Studio 11
MDST 3703 F10 Studio 11
 
Mdst 3559-01-27-data-journalism-studio
Mdst 3559-01-27-data-journalism-studioMdst 3559-01-27-data-journalism-studio
Mdst 3559-01-27-data-journalism-studio
 
Mdst 3559-01-20-intro
Mdst 3559-01-20-introMdst 3559-01-20-intro
Mdst 3559-01-20-intro
 
Mdst 3559-03-03-sql-php-2
Mdst 3559-03-03-sql-php-2Mdst 3559-03-03-sql-php-2
Mdst 3559-03-03-sql-php-2
 
MDST 3703 F10 Studio 6
MDST 3703 F10 Studio 6MDST 3703 F10 Studio 6
MDST 3703 F10 Studio 6
 
MDST 3703 F10 Studio 1
MDST 3703 F10 Studio 1MDST 3703 F10 Studio 1
MDST 3703 F10 Studio 1
 
MDST 3703 F10 Studio 3
MDST 3703 F10 Studio 3MDST 3703 F10 Studio 3
MDST 3703 F10 Studio 3
 
Presentation1
Presentation1Presentation1
Presentation1
 
Mdst 3559-03-15-web-apps
Mdst 3559-03-15-web-appsMdst 3559-03-15-web-apps
Mdst 3559-03-15-web-apps
 
MDST 3703 F10 Seminar 12
MDST 3703 F10 Seminar 12MDST 3703 F10 Seminar 12
MDST 3703 F10 Seminar 12
 
Mdst 3559-02-01-html
Mdst 3559-02-01-htmlMdst 3559-02-01-html
Mdst 3559-02-01-html
 
Mdst 3559-02-17-php2
Mdst 3559-02-17-php2Mdst 3559-02-17-php2
Mdst 3559-02-17-php2
 
MDST 3703 F10 Seminar 3
MDST 3703 F10 Seminar 3MDST 3703 F10 Seminar 3
MDST 3703 F10 Seminar 3
 
Mdst3703 2013-09-24-hypertext
Mdst3703 2013-09-24-hypertextMdst3703 2013-09-24-hypertext
Mdst3703 2013-09-24-hypertext
 
Mdst3559 2011-05-03-final-day
Mdst3559 2011-05-03-final-dayMdst3559 2011-05-03-final-day
Mdst3559 2011-05-03-final-day
 
MDST 3703 F10 Studio 4
MDST 3703 F10 Studio 4MDST 3703 F10 Studio 4
MDST 3703 F10 Studio 4
 

Ähnlich wie Mdst 3559-03-01-sql-php

Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in PerlLaurent Dami
 
Pdo – php database extension-Phpgurukul
Pdo – php database extension-PhpgurukulPdo – php database extension-Phpgurukul
Pdo – php database extension-PhpgurukulPHPGurukul Blog
 
Database presentation
Database presentationDatabase presentation
Database presentationwebhostingguy
 
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docxLab 2 Work with Dictionary and Create Relational Database (60 pts.).docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docxVinaOconner450
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptxnatesanp1234
 
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxmoirarandell
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingTricode (part of Dept)
 
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docxLab 2 Work with Dictionary and Create Relational Database (60 pts.docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docxDIPESH30
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentalsMadhuri Kavade
 
Connected data classes
Connected data classesConnected data classes
Connected data classesaspnet123
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan Samarasinghe
 

Ähnlich wie Mdst 3559-03-01-sql-php (20)

Sql
SqlSql
Sql
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
Pdo – php database extension-Phpgurukul
Pdo – php database extension-PhpgurukulPdo – php database extension-Phpgurukul
Pdo – php database extension-Phpgurukul
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docxLab 2 Work with Dictionary and Create Relational Database (60 pts.).docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.).docx
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
 
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
 
phptut4
phptut4phptut4
phptut4
 
phptut4
phptut4phptut4
phptut4
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docxLab 2 Work with Dictionary and Create Relational Database (60 pts.docx
Lab 2 Work with Dictionary and Create Relational Database (60 pts.docx
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Connected data classes
Connected data classesConnected data classes
Connected data classes
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
 

Mehr von Rafael Alvarado

Mdst3703 2013-10-08-thematic-research-collections
Mdst3703 2013-10-08-thematic-research-collectionsMdst3703 2013-10-08-thematic-research-collections
Mdst3703 2013-10-08-thematic-research-collectionsRafael Alvarado
 
Mdst3703 2013-10-01-hypertext-and-history
Mdst3703 2013-10-01-hypertext-and-historyMdst3703 2013-10-01-hypertext-and-history
Mdst3703 2013-10-01-hypertext-and-historyRafael Alvarado
 
Mdst3703 2013-09-12-semantic-html
Mdst3703 2013-09-12-semantic-htmlMdst3703 2013-09-12-semantic-html
Mdst3703 2013-09-12-semantic-htmlRafael Alvarado
 
Mdst3703 2013-09-17-text-models
Mdst3703 2013-09-17-text-modelsMdst3703 2013-09-17-text-models
Mdst3703 2013-09-17-text-modelsRafael Alvarado
 
Mdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signalsMdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signalsRafael Alvarado
 
Mdst3703 2013-09-05-studio2
Mdst3703 2013-09-05-studio2Mdst3703 2013-09-05-studio2
Mdst3703 2013-09-05-studio2Rafael Alvarado
 
Mdst3703 2013-09-03-plato2
Mdst3703 2013-09-03-plato2Mdst3703 2013-09-03-plato2
Mdst3703 2013-09-03-plato2Rafael Alvarado
 
Mdst3703 2013-08-29-hello-world
Mdst3703 2013-08-29-hello-worldMdst3703 2013-08-29-hello-world
Mdst3703 2013-08-29-hello-worldRafael Alvarado
 
UVA MDST 3703 2013 08-27 Introduction
UVA MDST 3703 2013 08-27 IntroductionUVA MDST 3703 2013 08-27 Introduction
UVA MDST 3703 2013 08-27 IntroductionRafael Alvarado
 
MDST 3705 2012-03-05 Databases to Visualization
MDST 3705 2012-03-05 Databases to VisualizationMDST 3705 2012-03-05 Databases to Visualization
MDST 3705 2012-03-05 Databases to VisualizationRafael Alvarado
 
Mdst3705 2013-02-26-db-as-genre
Mdst3705 2013-02-26-db-as-genreMdst3705 2013-02-26-db-as-genre
Mdst3705 2013-02-26-db-as-genreRafael Alvarado
 
Mdst3705 2013-02-19-text-into-data
Mdst3705 2013-02-19-text-into-dataMdst3705 2013-02-19-text-into-data
Mdst3705 2013-02-19-text-into-dataRafael Alvarado
 
Mdst3705 2013-02-12-finding-data
Mdst3705 2013-02-12-finding-dataMdst3705 2013-02-12-finding-data
Mdst3705 2013-02-12-finding-dataRafael Alvarado
 
Mdst3705 2013-02-05-databases
Mdst3705 2013-02-05-databasesMdst3705 2013-02-05-databases
Mdst3705 2013-02-05-databasesRafael Alvarado
 
Mdst3705 2013-01-29-praxis
Mdst3705 2013-01-29-praxisMdst3705 2013-01-29-praxis
Mdst3705 2013-01-29-praxisRafael Alvarado
 
Mdst3705 2013-01-31-php3
Mdst3705 2013-01-31-php3Mdst3705 2013-01-31-php3
Mdst3705 2013-01-31-php3Rafael Alvarado
 
Mdst3705 2012-01-22-code-as-language
Mdst3705 2012-01-22-code-as-languageMdst3705 2012-01-22-code-as-language
Mdst3705 2012-01-22-code-as-languageRafael Alvarado
 
Mdst3705 2013-01-24-php2
Mdst3705 2013-01-24-php2Mdst3705 2013-01-24-php2
Mdst3705 2013-01-24-php2Rafael Alvarado
 
Mdst3705 2012-01-15-introduction
Mdst3705 2012-01-15-introductionMdst3705 2012-01-15-introduction
Mdst3705 2012-01-15-introductionRafael Alvarado
 
Mdst3703 graph-theory-11-20-2012
Mdst3703 graph-theory-11-20-2012Mdst3703 graph-theory-11-20-2012
Mdst3703 graph-theory-11-20-2012Rafael Alvarado
 

Mehr von Rafael Alvarado (20)

Mdst3703 2013-10-08-thematic-research-collections
Mdst3703 2013-10-08-thematic-research-collectionsMdst3703 2013-10-08-thematic-research-collections
Mdst3703 2013-10-08-thematic-research-collections
 
Mdst3703 2013-10-01-hypertext-and-history
Mdst3703 2013-10-01-hypertext-and-historyMdst3703 2013-10-01-hypertext-and-history
Mdst3703 2013-10-01-hypertext-and-history
 
Mdst3703 2013-09-12-semantic-html
Mdst3703 2013-09-12-semantic-htmlMdst3703 2013-09-12-semantic-html
Mdst3703 2013-09-12-semantic-html
 
Mdst3703 2013-09-17-text-models
Mdst3703 2013-09-17-text-modelsMdst3703 2013-09-17-text-models
Mdst3703 2013-09-17-text-models
 
Mdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signalsMdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signals
 
Mdst3703 2013-09-05-studio2
Mdst3703 2013-09-05-studio2Mdst3703 2013-09-05-studio2
Mdst3703 2013-09-05-studio2
 
Mdst3703 2013-09-03-plato2
Mdst3703 2013-09-03-plato2Mdst3703 2013-09-03-plato2
Mdst3703 2013-09-03-plato2
 
Mdst3703 2013-08-29-hello-world
Mdst3703 2013-08-29-hello-worldMdst3703 2013-08-29-hello-world
Mdst3703 2013-08-29-hello-world
 
UVA MDST 3703 2013 08-27 Introduction
UVA MDST 3703 2013 08-27 IntroductionUVA MDST 3703 2013 08-27 Introduction
UVA MDST 3703 2013 08-27 Introduction
 
MDST 3705 2012-03-05 Databases to Visualization
MDST 3705 2012-03-05 Databases to VisualizationMDST 3705 2012-03-05 Databases to Visualization
MDST 3705 2012-03-05 Databases to Visualization
 
Mdst3705 2013-02-26-db-as-genre
Mdst3705 2013-02-26-db-as-genreMdst3705 2013-02-26-db-as-genre
Mdst3705 2013-02-26-db-as-genre
 
Mdst3705 2013-02-19-text-into-data
Mdst3705 2013-02-19-text-into-dataMdst3705 2013-02-19-text-into-data
Mdst3705 2013-02-19-text-into-data
 
Mdst3705 2013-02-12-finding-data
Mdst3705 2013-02-12-finding-dataMdst3705 2013-02-12-finding-data
Mdst3705 2013-02-12-finding-data
 
Mdst3705 2013-02-05-databases
Mdst3705 2013-02-05-databasesMdst3705 2013-02-05-databases
Mdst3705 2013-02-05-databases
 
Mdst3705 2013-01-29-praxis
Mdst3705 2013-01-29-praxisMdst3705 2013-01-29-praxis
Mdst3705 2013-01-29-praxis
 
Mdst3705 2013-01-31-php3
Mdst3705 2013-01-31-php3Mdst3705 2013-01-31-php3
Mdst3705 2013-01-31-php3
 
Mdst3705 2012-01-22-code-as-language
Mdst3705 2012-01-22-code-as-languageMdst3705 2012-01-22-code-as-language
Mdst3705 2012-01-22-code-as-language
 
Mdst3705 2013-01-24-php2
Mdst3705 2013-01-24-php2Mdst3705 2013-01-24-php2
Mdst3705 2013-01-24-php2
 
Mdst3705 2012-01-15-introduction
Mdst3705 2012-01-15-introductionMdst3705 2012-01-15-introduction
Mdst3705 2012-01-15-introduction
 
Mdst3703 graph-theory-11-20-2012
Mdst3703 graph-theory-11-20-2012Mdst3703 graph-theory-11-20-2012
Mdst3703 graph-theory-11-20-2012
 

Kürzlich hochgeladen

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Kürzlich hochgeladen (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

Mdst 3559-03-01-sql-php

  • 1. SQL + PHP MDST 3559: DataestheticsProf. Alvarado03/01/2011
  • 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)
  • 3. Review MySQL Importing data Creating tables and fields SQL SELECT statements
  • 4. SELECT SELECT [DISTINCT] f1, f2, f3, … FROM t1, t2, … WHERE (some condition is true) ORDERBY (one or more fields)
  • 5. JOINs Combines two or more tables. E.g.: SELECTcountry_debt.country_name, country_debt.debt, country_network.network FROMcountry_debt, country_network WHEREcountry_debt.country_name = country_network.country_name
  • 6. Table Aliases SELECTd.country_name, d.debt, n.network FROMcountry_debtd, country_networkn WHEREd.country_name= n.country_name
  • 7. Column Aliases SELECTd.country_nameas 'country', d.debt, n.network FROMcountry_debtd, country_networkn WHEREd.country_name= n.country_name
  • 8. Alternate JOIN Syntax SELECTd.country_name, d.debt, n.network FROMcountry_debtd JOINcountry_networkn ON (d.country_name = n.country_name)
  • 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
  • 14. Getting Started Create subdirectory 03-01 Create file lesson.php
  • 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