Sie sind auf Seite 1von 14

Introduction to

MySQL & PHP




PRESENTED BY,
M.PRAVIN RAJ
MySQL & PHP, presented by David Sands 2
History of SQL
1974 - First version of SQL developed by Donald Chamberlin and Raymond
Boyce at IBM (SEQUEL). Used to manipulate and retrieve data in their
database.
1986 - American National Standards Institute (ANSI) standardizes SQL-86.
1999 SQL3 supports new features like procedural & control-of-flow
statements, triggers, and regular expressions.
..
2008 SQL2008 - still modifying the language to date.
Popular SQL suppliers today
MySQL, 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 Public
License.
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: XXXXX
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server 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
Some MySQL + PHP Uses
Managing database from the web. Phymyadmin is a commonly used
remote 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 even
dynamically generate more web code.
Make and test your own website / program.
MySQL & PHP, presented by David Sands 8
PhpBB
MySQL & PHP, presented by David Sands 9
PhpMyAdmin
MySQL & PHP, presented by David Sands 10
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 11
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 12
Form Post Ex. (2 of 2)
MySQL & PHP, presented by David Sands 13
MySQL Functions
NOT NULL : The NOT NULL constraint enforces a
column to NOT accept NULL values.

UNIQUE Constraint : identifies each record in a
database table.

PRIMARY KEY Constraint : identifies each
record in a database table.


MySQL & PHP, presented by David Sands 14
MySQL Functions
FOREIGN KEY Constraint : A FOREIGN KEY in one table points
to a PRIMARY KEY in another table.

NOW() Function: returns the current system date and time.

FORMAT() Function : used to format how a field is to be displayed.
Ex:
SELECT ProductName, Price, FORMAT(Now(),'YYYY-MM-DD')
AS PerDate FROM Products;

Das könnte Ihnen auch gefallen