Download presentation
Presentation is loading. Please wait.
Published byArchibald French Modified over 9 years ago
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
2
SJSU CS157B Dr. Lee2 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
3
SJSU CS157B Dr. Lee3 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
4
SJSU CS157B Dr. Lee4 2004 Jenny Mitchell Developer Zone: Downloads and Documentation Available for Linux, Windows, Solaris, FreeBSD, and more MySQL.com
5
SJSU CS157B Dr. Lee5 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
6
SJSU CS157B Dr. Lee6 2004 Jenny Mitchell MySQL Terminal Example
7
SJSU CS157B Dr. Lee7 2004 Jenny Mitchell In-Class Demo Insertion MySQL Terminal Example
8
SJSU CS157B Dr. Lee8 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
9
SJSU CS157B Dr. Lee9 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.netwww.php.net Need your computer to act as a server –Apache, Tomcat are free downloads PHP
10
SJSU CS157B Dr. Lee10 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
11
SJSU CS157B Dr. Lee11 2004 Jenny Mitchell <?php $world = true; if ($world) echo(“Hello World!”); else echo(“Hello Nobody”); ?> Hello, World!
12
SJSU CS157B Dr. Lee12 2004 Jenny Mitchell <?php $world = false; if ($world) echo(“Hello World!”); else echo(“Hello Nobody”); ?> Hello Nobody
13
SJSU CS157B Dr. Lee13 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
14
SJSU CS157B Dr. Lee14 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(" Could not connect: ". mysql_error()); @mysql_select_db($db) or die(" Could not find database - $db - ". mysql_error(). " "); Connecting to MySQL from PHP
15
SJSU CS157B Dr. Lee15 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(" Query failed: ". mysql_error(). " "); Queries and Results
16
SJSU CS157B Dr. Lee16 2004 Jenny Mitchell $num = (int)@mysql_num_rows($result); echo “ ”; echo " ID Name Email "; for ($i = 0; $i < $num; $i++) { $line = mysql_fetch_array($result); echo " "; echo " ". $line[id]. " "; echo " ". $line[name]. " "; echo " ". $line[email]. " "; echo " "; } echo " "; /* Housekeeping */ mysql_free_result($result); mysql_close($dbc); Displaying Results
17
SJSU CS157B Dr. Lee17 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
18
SJSU CS157B Dr. Lee18 2004 Jenny Mitchell HTML FORM CODE Enter a column name by which to sort: 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(" Query failed: ". mysql_error(). " "); Selection and Variables Code
19
SJSU CS157B Dr. Lee19 2004 Jenny Mitchell In-Class Demo Data Mining from Forms Selection and Variables
20
SJSU CS157B Dr. Lee20 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
21
SJSU CS157B Dr. Lee21 2004 Jenny Mitchell HTML FORM CODE Insert some data into this database Name: Email: 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(" Query failed: ". mysql_error(). " "); $num = mysql_affected_rows(); if ($num == '1') echo " Data was inserted successfully"; Insertion with Forms Code
22
SJSU CS157B Dr. Lee22 2004 Jenny Mitchell In-Class Demo Inserting Data Insertion with Forms
23
SJSU CS157B Dr. Lee23 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…
24
SJSU CS157B Dr. Lee24 2004 Jenny Mitchell http://www.mysql.com http://www.php.net What Are You Waiting For? GO BUILD!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.