Professional Documents
Culture Documents
Outline
An introduction to MySQL
MySQL APIs
Using our MySQL server
Oracle vs. MySQL
PHP and MySQL
Operational logic
CRUD operations
Data retrieval
Error handling
www.bournemouth.ac.uk
An Introduction to MySQL
www.bournemouth.ac.uk
MySQL
Pronounced
My S Q L
www.bournemouth.ac.uk
MySQL Editions
MySQL Community Edition Free!
A excellent RDBMS with all the key
features most users require but it has no
customer support
There is however documentation and a users
forum
It is actually better than the standard
commercial version
www.bournemouth.ac.uk
Commercial Versions
MySQL Standard Edition ($2,000 a
year)
Basic database features with customer
support
The free community edition is actually
better
Web Architecture
Client
Server
HTTP
Web
front-end
(Apache)
User
Agent
HTTP
www.bournemouth.ac.uk
Middleware
(PHP)
API
Backend
(DB)
MySQL APIs
www.bournemouth.ac.uk
API Types
Abstraction layers:
Unify database interactions so that code
focuses on database functions (query,
insert, delete etc.) rather than individual
DBMSs.
This makes database code more flexible,
just change the configuration to use a
different DBMS and the code still works.
www.bournemouth.ac.uk
MySQL APIs
PHP has 3 different APIs to use MySQL:
mysql - Standard MySQL Extension
Deprecated,
DO mysql
NOT USE,
it will beuse
removed
Deprecated
- Do Not Use
extension,
mysqli or
PDO instead.
in PHP 7
www.bournemouth.ac.uk
www.bournemouth.ac.uk
More comparisons:
http://php.net/manual/en/mysqlinfo.api.choosing.php
http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-s
hould-you-use-net-24059
www.bournemouth.ac.uk
Connection details
To use our MySQL server (available
on Gauda) you need the following
details:
Host: 127.0.0.1
Username: Your regular username e.g.
i767654
Password: See your MYSQL-README file
See the MYSQL-README file in your home
directory (/home/ixxxx - note ixxx represents
your username)
www.bournemouth.ac.uk
www.bournemouth.ac.uk
I like/understand
Oracle, cant I
just use that?
www.bournemouth.ac.uk
Data Authentication
It is a process used to verify the
identity of a database user in order to
permit them to access, process or
alter data.
MySQLs implementation:
Grant tables are used to keep track of
users and the privileges that they can
have.
Location can be checked along with the
www.bournemouth.ac.uk
Schema
A database schema contains the definitions of
the tables, views, indexes, users, constraints,
stored procedures, triggers, and other
database-specific objects.
It can be considered as the header file of a
database which describes its structure (blueprint).
Data Types
Both MySQL and Oracle support three
main types: numeric, date and
time, and string.
The main difference is that data types
defined in MySQL are more specific.
For example:
MySQL: INT, DOUBLE, REAL and FLOAT.
Oracle: NUMBER(n,0), FLOAT(24).
www.bournemouth.ac.uk
SQL Queries
Both Oracle and MySQL are RDBMS
which support SQL queries.
However, they have different
implementations of SQL syntax.
Conceptually, they are the same but
practically they are different. Beware of
their differences.
Many DBMSs use their own
implementations of SQL - Notably
Oracle, Microsoft SQL Server and
www.bournemouth.ac.uk
Oracle
SELECT * FROM (SELECT columns FROM
table ORDER BY key ASC) WHERE ROWNUM
<= 5;
www.bournemouth.ac.uk
www.bournemouth.ac.uk
Connect to
the server
www.bournemouth.ac.uk
Select a
database
Submit a
query
Disconnect
from the
server
Retrieve
data
MySQLi Mapping
DB Operation
Logics
mysqli Extension
Server connection
mysqli_connect()
Database selection
mysqli_select_db()
Query submission
mysqli_query()
mysqli_multi_query()
Data retrieval
(fetch row or rows)
mysqli_fetch_assoc()
mysqli_fetch_array()
mysqli_fetch_all()
mysqli_free_result()
Server disconnection
mysqli_close()
Error handling
mysqli_error()
www.bournemouth.ac.uk
Server Connection
Syntax:
mysqli_connect(host, username,
password,
dbname,
port, socket);
Equivalent MySQL
command:
mysql h host u user p password db
Example:
$conn = mysqli_connect('127.0.0.1',
'i1234567',
For our server 'password','i1234567');
the database name is the same as the
username, in ordinary circumstances it would be
different.
www.bournemouth.ac.uk
Database Selection
Syntax:
mysqli_select_db(connection,dbname
);
Equivalent SQL syntax:
USE dbname;
Example:
$conn = mysqli_connect('127.0.0.1',
'i1234567',
'password');
mysqli_select_db($conn, 'mydb');
Note as you can specify the database when connecting to the
server using mysqli_connect() the only time you would
need to use this is to change databases after connecting.
www.bournemouth.ac.uk
Server Disconnection
Syntax:mysqli_close(dbconnection)
;
CRUD Operations
CRUD stands for Create, Read, Update
and Delete in relational database
applications.
It refers to INSERT/CREATE, SELECT,
UPDATE and DELETE/DROP SQL
statements.
CRUD operations are implemented as SQL
queries/commands.
www.bournemouth.ac.uk
Example:INT
VARCHAR (255)
p_stock
INT
Example:
Example:
p_name
p_stock
www.bournemouth.ac.uk
Example:
p_name
p_stock
21
www.bournemouth.ac.uk
Example:
$query = "DELETE FROM product
WHERE p_stock = 0;";
$result = mysqli_query($conn,
$query);
www.bournemouth.ac.uk
Example:
$query = "DROP TABLE IF EXISTS
products;";
$result = mysqli_query($conn, $query);
www.bournemouth.ac.uk
p_stock
10
11
12
13
Result set
Key
www.bournemouth.ac.uk
10
11
12
13
Value
10
Array1
Value
11
Array2
Value
12
Array3
mysqli_fetch_row(
);
www.bournemouth.ac.uk
www.bournemouth.ac.uk
www.bournemouth.ac.uk
www.bournemouth.ac.uk
www.bournemouth.ac.uk
Error Handling
Connection error: mysqli_connect()
It will return FALSE if connecting fails
The error can be captured using
mysqli_connect_error()
$conn =
mysqli_connect("127.0.0.1","username",
"password", "db");
if (!$conn) {
die(mysqli_connect_error());
}
echo "<p>Connected!</p>";
Shorthand
$conn =
mysqli_connect("127.0.0.1","username",
"password", "db") or
die(mysqli_connect_error());
www.bournemouth.ac.uk
$conn =
mysqli_connect("127.0.0.1","username",
"password", "db");
if (mysqli_connect_error()){
echo "Failed to connect to MySQL: .
mysqli_connect_error();
}
echo "<p>Connected!</p>";
www.bournemouth.ac.uk
Conclusion
www.bournemouth.ac.uk
Further reading
www.bournemouth.ac.uk
www.bournemouth.ac.uk