Lecture 15 - MySQL - PHP 1
Lecture 15 - MySQL - PHP 1
Multi-user capable
Multithreaded, multiprocessor, sometimes cluster
based systems
Standards based queries
Structured Query Language (SQL)
MySQL Database
world's most popular open source database
because of its consistent fast performance, high
reliability and ease of use
Open Source License:- free
GNU General Public License
Free to modify and distribute but all modification must
be available in source code format
Commercial:- not free
Fully paid up professional support
• used by Google, Facebook Nokia, YouTube,
Yahoo!, Alcatel-Lucent, Zappos.com, etc.
Basic Database Server Concepts
Database runs as a server
Attaches to either a default port or an administrator
specified port
Clients connect to database
For secure systems
authenticated connections
Database
Database Management User
Ssytem
os
Internet
Network Core
Server: responds
• Webserver supports HTTP.
Server
Web
server
My codes
MySQL PHP
HTTP HTML interpreter
Client
Operating System
Web
TCP/IP
browser
Internet
Server: responds
Internet
MySQL
Operating System Server
Web
TCP/IP
browser
Internet
• MySQL can be controlled through a
simple command-line interface; however,
we can use phpMyAdmin as an interface
to MySQL.
Server
Web
server
My codes
phpMy MySQL PHP
HTTP HTML Admin interpreter
Client
Operating System
Web
TCP/IP
browser
Internet
• A Quick Tour
Table: Customers (data)
Table: Products (data)
Table: Purchases (data)
Table: PurchaseProducts (data)
Database Design
In MySQL there are three main types :
• text
• number
• Date/Time.
CHAR(size) Holds a fixed length string (can contain letters, numbers, and special
characters). The fixed size is specified in parenthesis. Can store up to
255 characters
VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special
characters). The maximum size is specified in parenthesis. Can store up
to 255 characters. Note: If you put a greater value than 255 it will be
converted to a TEXT type
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT Holds a string with a maximum length of 65,535 characters
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
ENUM(x,y,z,etc.) Let you enter a list of possible values. You can list up to 65535 values in
an ENUM list. If a value is inserted that is not in the list, a blank value will
be inserted.Note: The values are sorted in the order you enter them.
You enter the possible values in this format: ENUM('X','Y','Z')
http://www.w3schools.com/sql/sql_datatypes.asp
TINYINT(size) -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits
may be specified in parenthesis
SMALLINT(size) -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number
of digits may be specified in parenthesis
MEDIUMINT(size) -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum
number of digits may be specified in parenthesis
INT(size) -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The
maximum number of digits may be specified in parenthesis
BIGINT(size) -9223372036854775808 to 9223372036854775807 normal. 0 to
18446744073709551615 UNSIGNED*. The maximum number of digits may
be specified in parenthesis
FLOAT(size,d) A small number with a floating decimal point. The maximum number of
digits may be specified in the size parameter. The maximum number of
digits to the right of the decimal point is specified in the d parameter
DOUBLE(size,d) A large number with a floating decimal point. The maximum number of
digits may be specified in the size parameter. The maximum number of
digits to the right of the decimal point is specified in the d parameter
DECIMAL(size,d) A DOUBLE stored as a string , allowing for a fixed decimal point. The
maximum number of digits may be specified in the size parameter. The
maximum number of digits to the right of the decimal point is specified in
the d parameter
http://www.w3schools.com/sql/sql_datatypes.asp
DATE() A date. Format: YYYY-MM-DDNote: The supported range is from '1000-01-
01' to '9999-12-31'
DATETIME() *A date and time combination. Format: YYYY-MM-DD HH:MM:SSNote: The
supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of seconds since
the Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD
HH:MM:SSNote: The supported range is from '1970-01-01 00:00:01' UTC to
'2038-01-09 03:14:07' UTC
http://www.w3schools.com/sql/sql_datatypes.asp
• A Quick Tour
Create Database
Create Table: Customers
Specify the Table’s Fields &
Attributes: Customers
Table Edit Screen: Customers
Table: Products
Table: Products
Insert Record: Customers
Table: Customers (data)
Insert Record: Products
Table: Products (data)
Edit Record
Export
Deleting a Table
Restoring a database from an SQL
file
Database Design
Summary
• Concept of databases
• Tables and Fields
• Field Types
• phpMyAdmin Tool for manipulating databases
• Creation of a database
• How to add and edit records
• How to back-up a database
• Database Design
Connecting to a MySQL DBMS
mysql_select_db(databasename, resourceId);
mysql_select_db("anotherdatabase", $dbLocalhost)
or die("Could not find database: " . mysql_error());
}
?>
array = mysql_fetch_row(resourceRecords)
require_once("database2.php");
Example15-12.php
Accessing Multiple Tables
<?php
// File: example15-13.php
require_once("database2.php");
echo "<p>Customers:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords["Title"] . " ";
echo $arrRecords["Surname"] . " ";
echo $arrRecords["Firstname"] . "</p>";
}
//...continued...
Example15-13.php
Accessing Multiple Tables
//continuation...
echo "<p>Products:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords["Name"] . " ";
echo $arrRecords["Description"] . " ";
echo $arrRecords["Quantity"] . " ";
echo $arrRecords["Cost"] . "</p>";
}
?>
Example15-13.php
Using records to read another table
Read a customer record, and then show the
products purchased by that customer.
Tables
• Customers
• Products
• Purchases
• PurchaseProducts
Example15-14.php
BIRD’S EYEVIEW
Example15-14.php
Complete version
$strSurname = "Jones";
Example15-14.php
Complete version
$arrProductRecord = mysql_fetch_array($dbProductRecords);
echo $arrProductRecord["Name"] . " (" . $arrProductRecord["Description"] . ") at £";
echo $arrProRecords["Cost"] . " each.</p>";
}
}
} Example15-14.php
Example15-14.php
?>
Inserting records
How to create new database records and insert
them into a table?
INSERT INTO table (field1, field2,...) VALUES (‘value1’, ‘value2’,...)
Example15-15.php
Inserting records
<?php
// File: example15-15.php
require_once("database2.php");
e.g.
Note: If you have a relational database, you should tidy-up the other tables, based on
their connection with the record you’ve deleted.
Example15-16.php
Deleting records
How to delete database records from tables?
Example15-17.php
Amending records
How to modify the contents of an existing
database record?
UPDATE table SET field=‘value1’, field=‘value2’...WHERE
field=‘value’
• requires you to specify the table, the list of fields with their
updated values, and a condition for selection (WHERE).
Example15-18.php
Amending records
<?php
// File: example15-18.php
require_once("database2.php");
Another Example:
$dbCustRecords = mysql_query("UPDATE products SET Name='Beer
and Lager Glass' WHERE Name='Beer Glass'", $dbLocalhost)
Example15-19.php
Counting the number of records
How to count the number of records after
running a query?
$dbProdRecords = mysql_query("SELECT * FROM products",
$dbLocalhost)
or die("Problem reading table: " . mysql_error());
$intProductCount = mysql_num_rows($dbProdRecords);
•This will return all records from the products table where the
first four characters in the name field equals ‘Wine’
Example15-22.php
End of Lecture