Introduction to MySQL & PHP Presented by D avid Sands C S 157B , SJSU Spring 09'
Introduction toMySQL & PHP
Presented by David SandsCS 157B, SJSU Spring 09'
MySQL & PHP, presented by David Sands 2
History of SQL
1974 - First version of SQL developed by Donald Chamberlin and RaymondBoyce at IBM (SEQUEL). Used to manipulate and retrieve data in theirdatabase.
1986 - American National Standards Institute (ANSI) standardizes SQL-86.
1999 – SQL3 supports new features like procedural & control-of-flowstatements, triggers, and regular expressions.…..2008 – SQL2008 - still modifying the language to date.
Popular SQL suppliers todayMySQL, Microsoft SQL Server, IBM DB2, Oracle 11g, PostgreSQLSQL
MySQL & PHP, presented by David Sands 3
Basic SQL Syntax
➔ Data Definition Language (DDL)
• CREATE TABLE / DATABASE / VIEW / etc.....• ALTER ...• DROP ...
➔ Data Manipulation Language (DML)
• SELECT ... FROM / INTO … WHERE ...• INSERT INTO ... VALUES ...• UPDATE … SET … WHERE ...• DELETE FROM … WHERE ...
MySQL & PHP, presented by David Sands 4
Intro to MySQL
➔ Released 23 May 1995.
➔ 11+ Million web servers using MySQL
➔ Similar, but not exactly same syntax as IBM DB2, Oracle 11g, etc...
➔ Open-source & free to download, under the GNU General PublicLicense.
➔ Coded in C / C++, Yacc parser, and custom lexical analyzer.
MySQL & PHP, presented by David Sands 5
MySQL Tutorial (1 of 2)
➔ Following from MySQL 5.1 Manual (3.3 Creating and using a database)➔ For Command Prompt usage, follow these steps to use a database.
Enter password: XXXXXWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.1.31-community MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.00 sec)mysql> USE TEST;Database changed
➔ You can now perform DML & DDL operations!
MySQL & PHP, presented by David Sands 6
MySQL Tutorial (2 of 2)
mysql> CREATE TABLE myTest (time DATE, note VARCHAR(10), id INT);Query OK, 0 rows affected (0.11 sec)
mysql> DESCRIBE myTest;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| time | date | YES | | NULL | || note | varchar(10) | YES | | NULL | || id | int(11) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+3 rows in set (0.05 sec)
mysql> INSERT INTO myTest VALUES (NULL, "hello", 3);Query OK, 1 row affected (0.05 sec)
mysql> SELECT * FROM myTest;+------+-------+------+| time | note | id |+------+-------+------+| NULL | hello | 3 |+------+-------+------+1 row in set (0.01 sec)
mysql>
MySQL & PHP, presented by David Sands 7
History of PHP
1994 - Rasmus Lerdorf wrote Common Gateway Interface (CGI) Binaries.
1995 - Personal Home Page Tools (PHP Tools) formed.
1997-8 - Zeev Suraski & Andi Gutmans wrote PHP parser. Their PHP3became the PHP: Hypertext Preprocessor.
To 2008 - Various improvements & bug fixes.
➔ Old versions of PHP – Code not compiled. Only interpreted and run.➔ After PHP4, Parser compiles input to produce bytecode for Zend Engineprocessing.
MySQL & PHP, presented by David Sands 8
Intro to PHP
➔ PHP file runs on web server, inputs PHP code, compiles to bytecode,outputs Web Pages.
➔ Creates Dynamic Web Pages, using Server Side Scripting. (like .asp, .jsp,perl). Clients “Run” these web pages when visited.
➔ Similar programming structure / syntax as C or Javascript. Other “Tools”included in PHP releases like ImageJPEG($im,$destpic,$jpeg_thumb_quality);
➔ HTML (markup language) usually used along with PHP.
MySQL & PHP, presented by David Sands 9
PHP Syntax
➔ <?php PHP code here ?>
➔ $variable //automatic type detection on assignment.
➔ $ is escape character for variables within double quotes➔ $newVar = “$string1 hihi!” //replaces $string1 with its value.➔ “Double quotes” = variable replacement➔ 'Single quotes' = literal string➔ That 3rd set of quotes (`???`) = some other use
➔ function generateThumbnail($sourceImg, $destImg){ }
MySQL & PHP, presented by David Sands 10
PHP Examples
MySQL & PHP, presented by David Sands 11
Some MySQL + PHP Uses
➔ Managing database from the web. Phymyadmin is a commonly usedremote database management system via a PHP interface.
➔ User places buy order on your website, and info is stored in DB.
➔ Progressively build a SQL Query string.
➔ PHP can blend with anything else on an HTML webpage, and evendynamically generate more web code.
➔ Make and test your own website / program.
MySQL & PHP, presented by David Sands 12
PhpBB
MySQL & PHP, presented by David Sands 13
PhpMyAdmin
MySQL & PHP, presented by David Sands 14
MySQL + PHP
Need a web server that connects to a local or remote Database?No problem!
Host most likely “localhost”.To perform SQL commands, try this php function...$sql = mysql_query(“SELECT * FROM myTable);Just like with Java and its JDBC.There is a way to iterate through the resulting bag.
MySQL & PHP, presented by David Sands 15
List all query results Ex.
MySQL & PHP, presented by David Sands 16
PHP Control Structure Ex.
PHP is much like C, Java, or Javascript in some ways.
This will print 0123456789
MySQL & PHP, presented by David Sands 17
Form Post Ex. (1 of 2)
• Test.php is purely HTML.• Form's POST action sends the object names to PHP file.• PHP file extracts them with array $_POST[].
MySQL & PHP, presented by David Sands 18
Form Post Ex. (2 of 2)
MySQL & PHP, presented by David Sands 19
How do I get PHP or MySQL?
Mysql.com (100 MB)php.net (for reference)
Some web server, like Apache or Wampserver will work.
For the examples, I used Wampserver (wampserver.com) (16 MB)1. Installed MySQL2. created some new tables with mysql3. installed Wampserver4. make .PHP files and put them in the www folder5. Go to http://localhost/my.php6. test your code.
MySQL & PHP, presented by David Sands 20
MySQL-PHP Conclusion
➔ MySQL is open-source and PHP is open-library.
➔ MySQL and PHP work well together.
➔ Free. Fairly simple and familiar to program with.
➔ MySQL is fast enough, but PHP is fairly slow.
➔ Many real-world applications on-line or even in a local network.
➔ If you are sick of MySQL command line, go get a web server withPhpMyAdmin.
MySQL & PHP, presented by David Sands 21
References
1. http://www.mysql.com2. http://www.php.net/mysql3. http://www.wampserver.com4. http://en.wikipedia.org/wiki/SQL5. http://en.wikipedia.org/wiki/Mysql6. http://en.wikipedia.org/wiki/Php