CS 46B: Introduction to Data Structures

CMPE 180-38
Database Systems Workshop
June 7 Class Meeting
Department of Computer Engineering
San Jose State University
Summer 2017
Instructor: Ron Mak
www.cs.sjsu.edu/~mak
MySQL Workbench

Open-source version of some very expensive
commercial database design and management
tools (such as ERWin Data Modeler).


Download from http://dev.mysql.com/downloads/
Features



Manage databases and database connections.
Edit, execute, and save SQL scripts.
Forward- and reverse-engineering.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
2
MySQL Workbench: Reverse Engineering

MySQL Workbench can generate
a new EER diagram by “reverse engineering”
an existing database.

An Extended ER (EER) diagram is a cross
between an ER diagram and a relational
schema diagram.

Demo: Generate a new EER diagram.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
3
MySQL Workbench: Forward Engineering

MySQL Workbench can generate
a new database by “forward engineering”
an EER diagram.

You can manually create an EER diagram
using the diagram editor.

Demo: Generate a new database.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
4
Self Join

A SELECT statement that joins a table to itself.

The table has a foreign key that refers to its
own primary key.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
5
Self Join, cont’d

Which corporate clients were referred by
other corporate clients?
SELECT c.ccname AS client, r.ccname AS recommender
FROM
corpclient c, corpclient r
WHERE r.ccid = c.ccidreferredby;
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
The use of aliases is
mandatory for a self join.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
6
Inner and Outer Joins

Inner join


What we’ve been doing so far.
Outer join



left outer join
right outer join
full outer join
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
7
Inner Join

Which apartments are rented
by which corporate clients?
SELECT a.buildingid, a.aptno, c.ccname
FROM
apartment a, corpclient c
WHERE a.ccid = c.ccid;
Retrieve only the
rows that match.
Computer Engineering Dept.
Summer 2017: June 7
Database Systems
CMPE 180-38: Database Systems Workshop by Jukić, Vrbsky, & Nestorov
© R. Mak
Pearson 2014
ISBN 978-0-13-257567-6
8
Left Outer Join
SELECT a.buildingid, a.aptno, c.ccname
FROM
apartment a LEFT OUTER JOIN corpclient c
ON
a.ccid = c.ccid;
Include all the rows
on the left part of the
relation (APARTMENT)
whether or not there is
a match.
Computer Engineering Dept.
Summer 2017: June 7
No matches.
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
9
Right Outer Join
SELECT a.buildingid, a.aptno, c.ccname
FROM
apartment a RIGHT OUTER JOIN corpclient c
ON
a.ccid = c.ccid;
Include all the rows
on the right part of the
relation (CORPCLIENT)
whether or not there is
a match.
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
No match.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
10
Full Outer Join
SELECT a.buildingid, a.aptno, c.ccname
FROM
apartment a FULL OUTER JOIN corpclient c
ON
a.ccid = c.ccid;
FULL OUTER JOIN
is not available with
MySQL.
Include all the rows
from both relations
whether or not there is
a match.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
11
IS NULL

Which managers did not get a bonus?
SELECT *
FROM
manager
WHERE mbonus IS NULL;
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
12
EXISTS

Follow WHERE EXISTS by a nested query.

The nested query is true if it retrieves any rows,
else it is false.

If the nested query uses columns from the
outer query, then the inner query is a
correlated subquery.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
13
EXISTS, cont’d

Which buildings have
managers living in them?
SELECT *
FROM
building b
WHERE EXISTS (SELECT *
FROM
manager m
correlated subquery WHERE b.buildingid
Computer Engineering Dept.
CMPE 180-38: Database Systems Workshop
Summer 2017: June 7
© R. Mak
= m.mresbuildingid);
14
NOT

Which buildings
do not have managers
living in them?
SELECT *
FROM building b
WHERE NOT EXISTS (SELECT *
FROM
manager m
WHERE b.buildingid
Computer Engineering Dept.
CMPE 180-38: Database Systems Workshop
Summer 2017: June 7
Mak
=© R.m.mresbuildingid);
15
INSERT INTO SELECT

Use the results from a query to populate
another table.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
16
INSERT INTO SELECT, cont’d
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6

Create a denormalized table.
CREATE TABLE cleaningdenormalized
(
buildingid CHAR(3)
NOT NULL,
aptno
CHAR(5)
NOT NULL,
smemberid
CHAR(4)
NOT NULL,
smembername VARCHAR(15) NOT NULL,
PRIMARY KEY (buildingid, aptno, smemberid)
);
INSERT INTO cleaningdenormalized
SELECT c.buildingid, c.aptno, s.smemberid, s.smembername
FROM
cleaning c,CMPE
staffmember
s
Computer Engineering Dept.
180-38: Database Systems Workshop
17
Summer 2017:c.smemberid
June 7
© R. Mak
WHERE
= s.smemberid;
Cascading Deletes

Normally, the referential integrity constraint
prevents you from deleting a record from a
table if it is referred to by a record in another
table.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
18
Cascading Deletes, cont’d
CREATE TABLE employee
(
empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON DELETE CASCADE
);
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
19
Cascading Deletes, cont’d
CREATE TABLE employee
(
empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON DELETE SET NULL
);
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
20
Cascading Updates
CREATE TABLE employee
(
empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON UPDATE CASCADE
);
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
21
Cascading Updates
CREATE TABLE employee
(
empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON UPDATE SET NULL
);
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
22
Indexing

Indexing increases the speed of retrieving table
rows based on an unsorted column.

Use a binary search instead of a linear search.
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
Linear search
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
23
Indexing (Conceptual View)
CREATE INDEX custname_index ON customer(custname);
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
24
Indexing (Conceptual View), cont’d
Binary search
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Database Systems
by Jukić, Vrbsky, & Nestorov
Pearson 2014
ISBN 978-0-13-257567-6
25
Indexing, cont’d

In an actual RDBMS, the index column can
contain physical disk addresses.

Indexing speeds record retrieval, but it will slow
record insertion, deletion, and modification.

To drop an index:
DROP INDEX custname_index;
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
26
MySQL Text Functions

Also:


SHA1(string)
One-way encryption
Returns a 40-character string.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
PHP and MySQL for
Dynamic Web Sites, 4th ed.
by Larry Ullman
Peachpit Press, 2012
ISBN 978-0-321-78407-0
27
MySQL Text Functions, cont’d
mysql> select concat(first, ' ', last)
-> from people;
+--------------------------+
| concat(first, ' ', last) |
+--------------------------+
| Charles Jones
|
| Mary Adams
|
| Susan Miller
|
| Roger Brown
|
| Leslie Adamson
|
+--------------------------+
5 rows in set (0.00 sec)
Computer Engineering Dept.
Summer 2017: June 7
mysql> select concat(first, ' ', last) as name
-> from people;
+----------------+
| name
|
+----------------+
| Charles Jones |
| Mary Adams
|
| Susan Miller
|
| Roger Brown
|
| Leslie Adamson |
+----------------+
CMPE 180-38: Database Systems Workshop
5 rows in set (0.00
© R. Mak sec)
28
MySQL Numeric Functions
SELECT CONCAT('$', FORMAT(5639.6, 2))
AS cost;
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
PHP and MySQL for
Dynamic Web Sites, 4th ed.
by Larry Ullman
Peachpit Press, 2012
ISBN 978-0-321-78407-0
29
MySQL Date and Time Functions
No arguments.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
PHP and MySQL for
Dynamic Web Sites, 4th ed.
by Larry Ullman
Peachpit Press, 2012
ISBN 978-0-321-78407-0
30
MySQL Date and Time Functions, cont’d



Data types that store both a date and time:
DATETIME and TIMESTAMP
Data type that stores just the date: DATE
Data type that stores just the year: YEAR
PHP and MySQL for
Dynamic Web Sites, 4th ed.
by Larry Ullman
Peachpit Press, 2012
ISBN 978-0-321-78407-0
SELECT DATE(registration_date)
AS Date FROM users ORDER BY
registration_date DESC LIMIT 1;
Computer Engineering Dept.
Summer 2017: June 7
SELECT DAYNAME(registration_date)
AS Weekday FROM users ORDER BY
registration_date ASC LIMIT 1;
CMPE 180-38: Database Systems Workshop
© R. Mak
31
Formatting the Date and Time
Return the current date and time as Month DD, YYYY - HH:MM :
PHP and MySQL for
Dynamic Web Sites, 4th ed.
by Larry Ullman
Peachpit Press, 2012
ISBN 978-0-321-78407-0
Select the email address and date registered,
ordered by date registered, formatting the date as
Weekday (abbreviated) Month (abbreviated) Day Year,
for the last five registered users :
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
32
Break
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
33
PHP Input Filtering

An optional third parameter specifies either a
sanitizing filter or a validation filter.

Example:
if (filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) {
echo("Email is valid");
}
else {
echo("Email is not valid");
}


A sanitizing filter strips off certain characters.
A validating filter checks the input for validity.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
34
PHP Input Filtering, cont’d
http://www.w3schools.com/php/php_ref_filter.asp
Filter constant
Default
ID
Description
FILTER_VALIDATE_BOOLEAN
258
Validates a boolean
FILTER_VALIDATE_EMAIL
274
Validates an e-mail address
FILTER_VALIDATE_FLOAT
259
Validates a float
FILTER_VALIDATE_INT
257
Validates an integer
FILTER_VALIDATE_IP
275
Validates an IP address
FILTER_VALIDATE_REGEXP
272
Validates a regular expression
FILTER_VALIDATE_URL
273
Validates a URL
FILTER_SANITIZE_EMAIL
517
Removes all illegal characters from an e-mail address
FILTER_SANITIZE_ENCODED
514
Removes/Encodes special characters
FILTER_SANITIZE_MAGIC_QUOTES
521
Apply addslashes()
FILTER_SANITIZE_NUMBER_FLOAT
520
Remove all characters, except digits, +- and optionally .,eE
FILTER_SANITIZE_NUMBER_INT
519
Removes all characters except digits and + -
FILTER_SANITIZE_SPECIAL_CHARS
515
Removes special characters
FILTER_SANITIZE_STRING
513
Removes tags/special characters from a string
FILTER_SANITIZE_STRIPPED
513
Alias of FILTER_SANITIZE_STRING
FILTER_SANITIZE_URL
518
Removes all illegal character from s URL
FILTER_UNSAFE_RAW
Computer
Engineering Dept.
Summer
2017:
June 7
FILTER_CALLBACK
516Database
Do nothing,
CMPE 180-38:
Systemsoptionally
Workshop strip/encode special
R. Mak
1024 ©Call
a user-defined function to filter data
characters
35
PHP is Object-Oriented

The object-oriented features and syntax
of PHP resemble those of Java:





classes and objects
abstract classes
inheritance
interfaces
PHP also has traits.

Add functionality to a class without inheritance.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
36
PHP Classes
class Pet
{
public $name;
oo/Pet.php
function __construct($pet_name)
{
$this->name = $pet_name;
}
The constructor
is always named
__construct
(two underscores).
function eat()
{ /* ... */ }
function sleep() { /* ... */ }
function play() { /* ... */ }
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
37
PHP Inheritance

As with Java, a PHP class can inherit
from at most one superclass.
class Cat extends Pet
{
function play()
{
parent::play(); Scope resolution
operator ::
}
}
oo/Pet.php
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
38
PHP Objects
$cat = new Cat('Eliza');
$pet = new Pet('Norska');
$cat->eat();
$pet->sleep();
// Delete the objects
unset($cat, $pet);
oo/Pet.php
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
39
PHP Abstract Classes
abstract class Shape
oo/Shape.php
{
abstract public function getArea();
abstract public function getPerimeter();
}
require('Shape.php');
oo/Triangle.php
class Triangle extends Shape
{
private $_sides = array();
private $_perimeter = NULL;
function __construct($s0 = 0, $s1 = 0, $s2 = 0)
{
/* ... */
}
public function getArea()
{ /* ... */ }
public
function
getPerimeter()
{ /*
... */ }
Computer Engineering
Dept.
CMPE 180-38: Database Systems
Workshop
}
Summer 2017: June 7
© R. Mak
40
PHP Interfaces
interface Crud
{
public function
public function
public function
public function
}
oo/Crud.php
create($data);
read();
update($data);
delete();
class User implements Crud
{
/* ... */
function create($data)
function read()
function update($data)
public function delete()
oo/Crud.php
{
{
{
{
/*
/*
/*
/*
...
...
...
...
*/
*/
*/
*/
}
}
}
}
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
41
PHP Traits

Traits add functionality to a class
without class inheritance.



They help overcome some of the
restrictions of single inheritance.
A class can use several traits.
Several classes can share traits.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
42
PHP Traits, cont’d
trait Debug
{
public function dumpObject()
{
$class = get_class($this);
$attributes = get_object_vars($this);
$methods = get_class_methods($this);
oo/Debug.php
echo "<h2>Information about the $class object</h2>";
echo '<h3>Attributes</h3><ul>';
foreach ($attributes as $k => $v) {
echo "<li>$k: $v</li>";
}
echo '</li></ul>';
echo '<h3>Methods</h3><ul>';
foreach ($methods as $v) {
echo "<li>$v</li>";
}
echo '</li></ul>';
}
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
43
PHP Traits, cont’d
require('Shape.php');
oo/Rectangle.php
class Rectangle extends Shape
{
use Debug;
public $width;
public $height;
function __construct($w, $h)
{
$width = $w;
$height = $h;
}
function getArea()
{ return $width * $height; }
function getPerimeter() { return 2*($width + $height); }
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
PHP Traits, cont’d
oo/traittest.php
require('Debug.php');
require('Rectangle.php');
$r = new Rectangle(42, 37);
$r->dumpObject();
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
45
Object-Relational Mapping (ORM)

Object-relational mapping (ORM) is a
set of techniques to overcome the mismatch
between objects and relational data.



objects: PHP data
relational data: database query results
PHP Data Object (PDO) can fetch data from
database tables in the form of objects.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
46
Object-Relational Mapping (ORM), cont’d
class Person
{
private $id;
private $first;
private $last;
private $gender;
private $salary;
public
public
public
public
public
function
function
function
function
function
getId()
getFirst()
getLast()
getGender()
getSalary()
{
{
{
{
{
return
return
return
return
return
$this->id; }
$this->first; }
$this->last; }
$this->gender; }
$this->salary; }
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
47
Object-Relational Mapping (ORM), cont’d
$ps = $con->prepare($query);
...
// Fetch the matching database table rows.
$ps->execute();
$ps->setFetchMode(PDO::FETCH_CLASS, "Person");
// Construct the HTML table row by row.
while ($person = $ps->fetch()) {
print "
<tr>\n";
print "
<td>" . $person->getId()
print "
<td>" . $person->getFirst()
print "
<td>" . $person->getLast()
print "
<td>" . $person->getGender()
print "
<td>" . $person->getSalary()
print "
</tr>\n";
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
.
.
.
.
.
Demo
"</td>\n";
"</td>\n";
"</td>\n";
"</td>\n";
"</td>\n";
48
Type Hinting

If you’ve defined a PHP class, you can “hint”
the class type in a function header.

PHP will generate a fatal error if you
pass a value with a type that doesn’t
match the type hint.

You cannot hint simple types
such as integer or string.
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
49
Type Hinting, cont’d
function createTableRow(Person
{
print "
<tr>\n";
print "
<td>" .
print "
<td>" .
print "
<td>" .
print "
<td>" .
print "
<td>" .
print "
</tr>\n";
}
$p)
$p->getId()
$p->getFirst()
$p->getLast()
$p->getGender()
$p->getSalary()
.
.
.
.
.
"</td>\n";
"</td>\n";
"</td>\n";
"</td>\n";
"</td>\n";
// Construct the HTML table row by row.
while ($person = $ps->fetch()) {
createTableRow($person);
}
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Demo
50
Namespaces

A PHP namespace is similar to a Java package.

Separate nested namespace names
with \ instead of .

By convention, namespace names
are also source directory names.
namespace Shapes\Simple;
class Rectangle …
class Square …
class Circle …
Computer Engineering Dept.
Summer 2017: June 7
require('Shapes/Simple/Circle.php');
…
new Shapes\Simple\Circle(…);
require('Shapes/Simple/Circle.php');
use Shapes\Simple
…
new Circle(…);
CMPE 180-38: Database Systems Workshop
© R. Mak
51
PHP Error Reporting

Run phpInfo() and check that flag
display_errors is on during development.

Set in the PHP configuration file
XAMPP/xamppfiles/etc/php.ini
display_errors=On

Set at run time:
ini_set('display_errors', 1);


0: off
1: on
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
52
PHP Error Reporting, cont’d
errors/display.php
<body>
<h1>Display Errors</h1>
<?php
ini_set('display_errors', 1);
// Create errors
foreach ($var as $v) {}
$result = 1/0;
?>
</body>
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Demo
53
PHP Error Reporting, cont’d

Set the error-reporting level.
errors/levels.php
<body>
<h1>Error Levels</h1>
<?php
ini_set('display_errors', 1);
// Adjust error reporting
error_reporting(E_WARNING | E_STRICT);
// Create errors
foreach ($var as $v) {}
$result = 1/0;
?>
</body>
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Demo
54
PHP Error Reporting, cont’d
http://www.w3schools.com/php/php_ref_error.asp
Value Constant
1 E_ERROR
2 E_WARNING
4 E_PARSE
Description
Fatal run-time errors. Errors that cannot be recovered from. Execution of the script is halted
Run-time warnings (non-fatal errors). Execution of the script is not halted
Compile-time parse errors. Parse errors should only be generated by the parser
Run-time notices. The script found something that might be an error, but could also happen when
8 E_NOTICE
running a script normally
16 E_CORE_ERROR
Fatal errors at PHP startup. This is like E_ERROR, except it is generated by the core of PHP
32 E_CORE_WARNING
Non-fatal errors at PHP startup. This is like E_WARNING, except it is generated by the core of PHP
64 E_COMPILE_ERROR
Fatal compile-time errors. This is like E_ERROR, except it is generated by by the Zend Scripting Engine
Non-fatal compile-time errors. This is like E_WARNING, except it is generated by by the Zend Scripting
128 E_COMPILE_WARNING
Engine
Fatal user-generated error. This is like E_ERROR, except it is generated in PHP code by using the PHP
256 E_USER_ERROR
function trigger_error()
Non-fatal user-generated warning. This is like E_WARNING, except it is generated in PHP code by
512 E_USER_WARNING
using the PHP function trigger_error()
User-generated notice. This is like E_NOTICE, except it is generated in PHP code by using the PHP
1024 E_USER_NOTICE
function trigger_error()
Enable to have PHP suggest changes to your code which will ensure the best interoperability and
2048 E_STRICT
forward compatibility of your code (Since PHP 5 but not included in E_ALL until PHP 5.4)
Catchable fatal error. Indicates that a probably dangerous error occurred, but did not leave the Engine
4096 E_RECOVERABLE_ERROR in an unstable state. If the error is not caught by a user defined handle, the application aborts as it
was an E_ERROR (Since PHP 5.2)
Run-time notices. Enable this to receive warnings about code that will not work in future versions
8192 E_DEPRECATED
(Since PHP 5.3)
User-generated warning message. This is like E_DEPRECATED, except it is generated in PHP code by
Computer Engineering Dept.
CMPE 180-38: Database Systems Workshop
16384 E_USER_DEPRECATED
55
(Since PHP 5.3)
Summer 2017: June 7 using the PHP function trigger_error()
© R. Mak
32767 E_ALL
Enable all PHP errors and warnings (except E_STRICT in versions < 5.4)
PHP Error Suppression

Suppress individual errors with @

Example: Don’t report a missing file error:
@include 'testbedstart.inc.php';

Example: Don’t report a division by zero:
$result = @(1/0);
errors/suppress.php
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
Demo
56
Custom PHP Error Handler

Write a custom error handler with the signature:
functionName($level, $message, $file, $line, $context)

Then tell PHP to use it:
set_error_handler ('functionName');
Computer Engineering Dept.
Summer 2017: June 7
CMPE 180-38: Database Systems Workshop
© R. Mak
57
Custom PHP Error Handler, cont’d
errors/custom.php
define('LIVE', FALSE);
function myErrorHandler ($number, $message, $file, $line, $vars)
{
if (!LIVE) {
$message = "<hr /><strong>An error occurred in file '$file' " .
"on line $line: $message</strong>\n<pre>\n" .
print_r($vars, 1) . "</pre>";
echo "$message\n";
echo "<br /><strong>Backtrace:</strong><br />\n";
debug_print_backtrace();
echo "<hr />";
}
else {
echo '<div class="error">'.
'
A system error occurred. ' .
'
We apologize for the inconvenience.' .
'</div><br />';
}
}
Computer Engineering Dept.
CMPE 180-38: Database Systems Workshop
set_error_handler
Summer 2017: June 7 ('myErrorHandler');
© R. Mak
Demo
58