Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny...
-
Upload
archibald-french -
Category
Documents
-
view
214 -
download
0
Transcript of SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny...
![Page 1: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/1.jpg)
SJSU CS157B Dr. Lee 1 2004 Jenny Mitchell
Two Useful Tools You Can’t Live Without
by Jenny MitchellSJSU CS157B Section 1
09-21-04
PHP and MySQL
![Page 2: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/2.jpg)
SJSU CS157B Dr. Lee 2 2004 Jenny Mitchell
• We are learning about databases, now what can we do with that knowledge?
• Oracle– Large download and installation– Most of us used Oracle 9 in 157A
• MySQL– Small download or use from web server– Same SQL syntax, run from command line
or other application
Putting Our Knowledge to Use
![Page 3: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/3.jpg)
SJSU CS157B Dr. Lee 3 2004 Jenny Mitchell
• GNU Free, open-source, multi-platform
• Implements the standard SQL language
• Relational DBMS
• Written in C and C++
• Uses MyISAM B-tree disk tables
• Uses in-memory hash tables for temporary tables
About MySQL
![Page 4: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/4.jpg)
SJSU CS157B Dr. Lee 4 2004 Jenny Mitchell
• Developer Zone: Downloads and Documentation• Available for Linux, Windows, Solaris, FreeBSD,
and more
MySQL.com
![Page 5: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/5.jpg)
SJSU CS157B Dr. Lee 5 2004 Jenny Mitchell
• Download 4.0 binary packages
• Check installation documents to see what is automatically installed where
• Add appropriate aliases and users
• First start up in safe mode and test default tables
• Run from shell or application
MySQL Installation
![Page 6: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/6.jpg)
SJSU CS157B Dr. Lee 6 2004 Jenny Mitchell
MySQL Terminal Example
![Page 7: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/7.jpg)
SJSU CS157B Dr. Lee 7 2004 Jenny Mitchell
In-Class Demo
Insertion
MySQL Terminal Example
![Page 8: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/8.jpg)
SJSU CS157B Dr. Lee 8 2004 Jenny Mitchell
• What languages can we use for building an application on MySQL?– ODBC Connector– JDBC Connector– .NET Connector– Web scripting languages
• PHP, ASP, JSP
Building On MySQL
![Page 9: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/9.jpg)
SJSU CS157B Dr. Lee 9 2004 Jenny Mitchell
• PHP: Hypertext Processor• Open source scripting language for web
developers to write dynamically generated webpages
• Can download and install source and binaries from www.php.net
• Need your computer to act as a server – Apache, Tomcat are free downloads
PHP
![Page 10: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/10.jpg)
SJSU CS157B Dr. Lee 10 2004 Jenny Mitchell
• Syntax similar to C and JavaScript
• Not a compiled language, interpreted– You don’t know where an error is until you try to
run it, and even then the line number doesn’t always tell you where the actual error is
• Same logical structure - if/else, for, while
• Variables have no type declaration
Basic PHP Scripting
![Page 11: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/11.jpg)
SJSU CS157B Dr. Lee 11 2004 Jenny Mitchell
<?php
$world = true;
if ($world)
echo(“Hello World!”);
else
echo(“Hello Nobody”);
?>
Hello, World!
![Page 12: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/12.jpg)
SJSU CS157B Dr. Lee 12 2004 Jenny Mitchell
<?php
$world = false;
if ($world)
echo(“Hello World!”);
else
echo(“Hello Nobody”);
?>
Hello Nobody
![Page 13: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/13.jpg)
SJSU CS157B Dr. Lee 13 2004 Jenny Mitchell
• Anything in a MySQL database can be viewed on a webpage
• Any information which can be captured from a website can be stored into a database
FORMS
Uses of PHP and MySQL
![Page 14: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/14.jpg)
SJSU CS157B Dr. Lee 14 2004 Jenny Mitchell
/* Connection */
$username = ‘user’; // username
$password = ’password'; // password
$webhost = 'localhost'; // host (localhost or something on a web server)
$db = ’test'; // the database
$dbc = mysql_connect($webhost, $username, $password) or die("<p>Could not connect: <i>" . mysql_error());
@mysql_select_db($db) or die("<p>Could not find database - $db - <i>" . mysql_error() . "</i>");
Connecting to MySQL from PHP
![Page 15: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/15.jpg)
SJSU CS157B Dr. Lee 15 2004 Jenny Mitchell
/* Perform SQL query and catch data */
$query = "SELECT * from t1 WHERE email != ‘’ ORDER BY name ASC";
$result = mysql_query($query) or die("<p>Query failed: <i>" . mysql_error() . "</i>");
Queries and Results
![Page 16: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/16.jpg)
SJSU CS157B Dr. Lee 16 2004 Jenny Mitchell
$num = (int)@mysql_num_rows($result);
echo “<table border=2 cellspacing=3>”;echo "<tr><td><b>ID</b></td><td><b>Name</b></td><td><b>Email</b></td></tr>";
for ($i = 0; $i < $num; $i++){
$line = mysql_fetch_array($result);echo "<tr>";echo "<td>" . $line[id] . "</td>";echo "<td>" . $line[name] . "</td>";echo "<td>" . $line[email] . "</td>";echo "</tr>";
}echo "</table>";
/* Housekeeping */mysql_free_result($result);mysql_close($dbc);
Displaying Results
![Page 17: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/17.jpg)
SJSU CS157B Dr. Lee 17 2004 Jenny Mitchell
• SQL Query statements can be built over time
• PHP variables can be used in SQL Query statements
• Variables can be generated based on input form values
Selection and Variables
![Page 18: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/18.jpg)
SJSU CS157B Dr. Lee 18 2004 Jenny Mitchell
HTML FORM CODE
<form method=post action=testinputquery.php><p>Enter a column name by which to sort: <input type=text size=8 maxlength=8
name=column><p><input type=Submit value=Submit name=Submit><input type=Reset value=Reset></form>
SQL PROCESSING CODE
/* Gather data from form variable */$column = $_POST['column'];
/* Perform SQL query to catch data */$query = "SELECT * from t1 WHERE email != '' ORDER BY $column ASC";
$result = mysql_query($query) or die("<p>Query failed: <i>" . mysql_error() . "</i>");
Selection and Variables Code
![Page 19: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/19.jpg)
SJSU CS157B Dr. Lee 19 2004 Jenny Mitchell
In-Class Demo
Data Mining from Forms
Selection and Variables
![Page 20: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/20.jpg)
SJSU CS157B Dr. Lee 20 2004 Jenny Mitchell
• Create form with all input variables you want, submit action to someform.php
• PHP script grabs all “post”ed variables in the form
$var = $_POST[‘var’];
• Note that empty variables are the empty string and not the null value
Insertion with Forms
![Page 21: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/21.jpg)
SJSU CS157B Dr. Lee 21 2004 Jenny Mitchell
HTML FORM CODE
<form method=post action=testinsertion.php><p>Insert some data into this database</p><p><b>Name:</b> <input type=text name=name size=15 maxlength=20><p><b>Email:</b> <input type=text name=email size=15 maxlength=25><p><input type=Submit value=Submit name=Submit> <input type=Reset value=Reset name=Reset>
</form>
SQL PROCESSING CODE
/* Gather data from form variable */$name = $_POST['name']; $email = $_POST['email'];
/* Perform SQL query to catch data */$query = "INSERT INTO t1(name, email) values(\"$name\", \"$email\")";
mysql_query($query) or die("<p>Query failed: <i>" . mysql_error() . "</i>");$num = mysql_affected_rows();
if ($num == '1')echo "<p>Data was inserted successfully";
Insertion with Forms Code
![Page 22: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/22.jpg)
SJSU CS157B Dr. Lee 22 2004 Jenny Mitchell
In-Class Demo
Inserting Data
Insertion with Forms
![Page 23: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/23.jpg)
SJSU CS157B Dr. Lee 23 2004 Jenny Mitchell
• MySQL - relational database package which is free, small, easy to install
• PHP - dynamic scripting language which is free, small, easy to install, and automatically works with MySQL
• MySQL + PHP = affordable, portable, easily accessible database backend & application frontend
In Conclusion…
![Page 24: SJSU CS157B Dr. Lee1 2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section 1 09-21-04 PHP and MySQL.](https://reader035.fdocuments.in/reader035/viewer/2022070417/56649de45503460f94adbbc9/html5/thumbnails/24.jpg)
SJSU CS157B Dr. Lee 24 2004 Jenny Mitchell
• http://www.mysql.com
• http://www.php.net
What Are You Waiting For?
GO BUILD!