Upload
johnathan-jones
View
227
Download
0
Embed Size (px)
Citation preview
Database APIs and Wrappers
MySQL, MySQLi, PDO, PG, OCI, SQLSRV
Ivan YonkovTechnical Trainerhttp://github.com/RoYaLBGSoftware Universityhttp://softuni.bg
Web Development Basics
Table of Contents
1. What is a DB API?
2. Various DB Extensions in PHP
1. Mysql – deprecated
2. Mysqli
3. Pg – Postgre SQL
4. Oci – Oracle
5. PDO – Various DB access interface
3. Using PDO2
Database API
What is Database API?
4
Database API is the public interface provided to communicate with certain database. It could be a set of functions, classes and methods which perform actions against the database through a desired language.
Various Database Extensions
Various Database API’s PHP can natively communicate to a lot of databases through its
standard library (yet some extensions are required) MySQL database
Oracle database
Postgre database
SQLite database
MS SQL Server database
Etc…
6
MySQL Extension The MySQL Extension is the oldest API for communicating to
MySQL Server It’s been there for more than 15 years (PHP 4.0), marked as
deprecated and removed as of PHP 7.0 Uses standard php global functions prefixed with mysql_ Communicates natively with MySQL and sends plain queries Very vulnerable and error prone
7
MySQL Extension (2)
8
mysql_connect(“localhost”, “root”, “”, “forum”);
$result = mysql_query(“SELECT * FROM topics”);
while ($row = mysql_fetch_assoc($result)) {echo $row[‘body’] . “<br />”;
}
MySQLi Extension The MySQLi Extension is the de facto PHP’s standard succeedor
of the MySQL extension Provides both Object oriented and Procedural style
OOP way – the mysqli class. Procedural – the mysql_ prefix
Still uses old PHP style method naming (under_scored) Supports a way of parameter binding
9
MySQLi Extension (2) [Procedural]
10
$conn = mysqli_connect(“localhost”, “root”, “”, “forum”);
$result = mysqli_query($link, “SELECT * FROM topics”);
while ($row = mysqli_fetch_assoc($result) {echo $row[‘body’] . “<br/>”;
}
MySQLi Extension (3) [OOP]
11
$mysqli = new mysqli(“localhost”, “root”, “”, “forum”);
$result = $mysqli->query(“SELECT * FROM topics”);
while ($row = $result->fetch_assoc()) {echo $row[‘body’] . “<br/>”;
}
PDO
PDO PDO (PHP Data Objects) like Java Data Objects, provides consistent
interface for accessing different databases through db drivers Has full OOP support Supports prepared statements Good method naming and consistent objects Can reuse code after driver changes if the SQL dialect is the same
One can use the same methods no matter the driver Does not rewrite SQL or missing features
13
PDO (2)
14
$dbh = new PDO($dsn, $user, $pass);
The DSN is a connection string providing the driver, host and database name if present. MySQL dsn: “mysql:host=localhost;dbname=forum” SQLite dsn
File: “sqlite:/home/royal/databases/sqlite/mysqlitedb.sq3” Memory: “sqlite::memory:”
PDO (3)
15
$dbh = new PDO(“mysql:host=localhost;dbname=forum”, “root”, “”
);
PDO (4)
16
/** @var \PDOStatement $sth */$id = $_GET[‘id’];$sth = $db->prepare(“SELECT * FROM topics WHERE id = ?”);$sth->bindParam(1, $id, PDO::PARAM_INT);$sth->execute();$topic = $sth->fetch();echo $topic[‘body’];
17
Summary
1. What is a DB API?
2. Various DB Extensions in PHP
1. Using mysql_* extension (don’t)
2. Using mysqli_* extension
3. Using PDO
?
??
?
?
??
?
?
Questions?
Database APIs and Wrappers
https://softuni.bg/courses/web-development-basics/
License
This course (slides, examples, demos, videos, homework, etc.)is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license
19
Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
"C# Part I" course by Telerik Academy under CC-BY-NC-SA license
Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education,
Profession and Job for Software Developers softuni.bg
Software University @ Facebook facebook.com/SoftwareUniversity
Software University @ YouTube youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg