20
Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer http://github.com/ RoYaLBG Software University http:// softuni.bg Web Development Basics

Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer Software University

Embed Size (px)

Citation preview

Page 1: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

Database APIs and Wrappers

MySQL, MySQLi, PDO, PG, OCI, SQLSRV

Ivan YonkovTechnical Trainerhttp://github.com/RoYaLBGSoftware Universityhttp://softuni.bg

Web Development Basics

Page 2: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 3: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

Database API

Page 4: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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.

Page 5: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

Various Database Extensions

Page 6: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 7: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 8: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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 />”;

}

Page 9: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 10: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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/>”;

}

Page 11: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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/>”;

}

Page 12: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

PDO

Page 13: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 14: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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:”

Page 15: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

PDO (3)

15

$dbh = new PDO(“mysql:host=localhost;dbname=forum”, “root”, “”

);

Page 16: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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’];

Page 17: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 19: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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

Page 20: Database APIs and Wrappers MySQL, MySQLi, PDO, PG, OCI, SQLSRV Ivan Yonkov Technical Trainer  Software University

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