PHP supports many database systems including dBase, DBM, FilePro, mSQL, MySQL, ODBC, Oracle, and Sybase. It also supports systems that act somewhat as databases including IMAP, LDAP, and SNMP.
What is SQL?
SQL stands for Structured Query Language. It is the basis of most current relational database systems including mSQL, MySQL, Oracle, and Sybase. PHP also can be used with MS SQL Server and Access.
Using SQL with PHP
Here are a few examples of MySQL commands and the corresponding PHP functions.
The command to start a MySQL session:
cree> mysql
The PHP function call to open a MySQL connection:
int mysql_connect(string hostname, string username, string password);
The MySQL command to create a database:
mysql> CREATE DATABASE mydb;
The PHP function call:
mysql_create_db(string mydb, int linkid);
The MySQL command to specify a database:
mysql> USE mydb;
The PHP function call:
mysql_select_db(string mydb, int linkid);
A Simple Example
From [Atkinson], a MySQL example creating a table:
CREATE TABLE catalog
(
ID INT(11) NOT
NULL AUTO_INCREMENT,
Name VARCHAR(32),
Price FLOAT(6,2),
PRIMARY KEY
(ID)
);
SHOW TABLES;
SHOW COLUMNS FROM
catalog;
initializing values:
INSERT INTO catalog (Name, Price)
VALUES ('Toothbrush', 1.79);
INSERT INTO catalog (Name, Price)
VALUES ('Comb', 0.95);
INSERT INTO catalog (Name, Price)
VALUES ('Toothpaste', 5.39);
INSERT INTO catalog (Name, Price)
VALUES ('Dental Floss', 3.50);
INSERT INTO catalog (Name, Price)
VALUES ('Shampoo', 2.50);
INSERT INTO catalog (Name, Price)
VALUES ('Conditioner', 3.15);
INSERT INTO catalog (Name, Price)
VALUES ('Deodorant', 1.50);
INSERT INTO catalog (Name, Price)
VALUES ('Hair Gel', 6.25);
INSERT INTO catalog (Name, Price)
VALUES ('Razor Blades', 2.99);
INSERT INTO catalog (Name, Price)
VALUES
('Brush', 1.15);
and retrieving values:
SELECT * FROM catalog;
| ID | Name
| Price |
| 1 | Toothbrush
| 1.79 |
| 2 | Comb
| 0.95 |
| 3 | Toothpaste
| 5.39 |
| 4 | Dental Floss |
3.50 |
| 5 | Shampoo
| 2.50 |
| 6 | Conditioner |
3.15 |
| 7 | Deodorant
| 1.50 |
| 8 | Hair Gel
| 6.25 |
| 9 | Razor Blades |
2.99 |
| 10 | Brush
| 1.15 |
10 rows in set (0.02 sec)
Then, using PHP to access the database
PHP/16-4.php
(source PHP/16-4.txt).