Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database APIs and Wrappers

Similar presentations


Presentation on theme: "Database APIs and Wrappers"— Presentation transcript:

1 Database APIs and Wrappers
MySQL, MySQLi, PDO, PG, OCI, SQLSRV Web Development Basics Ivan Yonkov Technical Trainer Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Contents What is a DB API? Various DB Extensions in PHP
Mysql – deprecated Mysqli Pg – Postgre SQL Oci – Oracle PDO – Various DB access interface Using PDO © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

3 Database API © Software University Foundation – http://softuni.org
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

4 What is Database API? 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. © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

5 Various Database Extensions
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

6 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… © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

7 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 © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

8 MySQL Extension (2) mysql_connect(“localhost”, “root”, “”, “forum”);
$result = mysql_query(“SELECT * FROM topics”); while ($row = mysql_fetch_assoc($result)) { echo $row[‘body’] . “<br />”; } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

9 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 © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

10 MySQLi Extension (2) [Procedural]
$conn = mysqli_connect(“localhost”, “root”, “”, “forum”); $result = mysqli_query($link, “SELECT * FROM topics”); while ($row = mysqli_fetch_assoc($result) { echo $row[‘body’] . “<br/>”; } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

11 MySQLi Extension (3) [OOP]
$mysqli = new mysqli(“localhost”, “root”, “”, “forum”); $result = $mysqli->query(“SELECT * FROM topics”); while ($row = $result->fetch_assoc()) { echo $row[‘body’] . “<br/>”; } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

12 PDO © Software University Foundation – http://softuni.org
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

13 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 © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

14 PDO (2) $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:” © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

15 PDO (3) $dbh = new PDO( “mysql:host=localhost;dbname=forum”, “root”,
“” ); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

16 PDO (4) /** @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’]; © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

17 Summary What is a DB API? Various DB Extensions in PHP Using PDO
Using mysql_* extension (don’t) Using mysqli_* extension Using PDO © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

18 Database APIs and Wrappers
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

19 License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license 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 © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

20 Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Database APIs and Wrappers"

Similar presentations


Ads by Google