Slide 1

Slide 1 text

Databases and PHP Justin Yost Engineering Manager Wirecutter 1 CC BY-NC 4.0 Justin Yost

Slide 2

Slide 2 text

PHP and Your Computer • Easy all in one tool: https:/ /ampps.com/ • Modern Dev Tooling: https:/ /github.com/sprintcube/docker- compose-lamp • Cross Platform with a Framework: https:/ /laravel.com/docs/8.x/ homestead 2 CC BY-NC 4.0 Justin Yost

Slide 3

Slide 3 text

PHP and MySQL Running • php -v • mysql --version 3 CC BY-NC 4.0 Justin Yost

Slide 4

Slide 4 text

View Database 4 CC BY-NC 4.0 Justin Yost

Slide 5

Slide 5 text

Connect and Close 5 CC BY-NC 4.0 Justin Yost

Slide 6

Slide 6 text

Connect, Read and Close 6 CC BY-NC 4.0 Justin Yost

Slide 7

Slide 7 text

Connect, Write and Close 7 CC BY-NC 4.0 Justin Yost

Slide 8

Slide 8 text

MySQLi and PDO • MySQLi - Create a connection to a MySQL database that you can use in PHP (procedural and object-oriented patterns) • PDO - PHP Database Object, a consistent interface that enables you to connect to a variety of databases using a single pattern. 8 CC BY-NC 4.0 Justin Yost

Slide 9

Slide 9 text

PDO • PDO is the choice you should be making to connect to a database. • PDO includes drivers for; MySQL, MS SQL, PostgreSQL, SQLite and more • Data Access Abstraction Layer - same PDO function to connect, run statements, return results, etc • Does not abstract away differences in the actual query language or engine 9 CC BY-NC 4.0 Justin Yost

Slide 10

Slide 10 text

PDO Connect and Close 10 CC BY-NC 4.0 Justin Yost

Slide 11

Slide 11 text

PDO Connect, Read and Close 11 CC BY-NC 4.0 Justin Yost

Slide 12

Slide 12 text

PDO Connect, Write and Close 12 CC BY-NC 4.0 Justin Yost

Slide 13

Slide 13 text

Prepared Statements • Enables you to create statements that have placeholders, either named with a :name style or ? style • Can't mix and match the style of placeholders (go with named it'll make your life easier) • Bind user input to the placeholder, prevents the possibility of user input breaking your statements. • Don't quote the placeholder, just drop them in PDO will figure it out 13 CC BY-NC 4.0 Justin Yost

Slide 14

Slide 14 text

Going beyond this • ORMs - Object Relational Mapping • ORMs are present in frameworks, they provide an abstraction above and beyond PDO • PDO minimizes differences in executing raw SQL against a database engine • ORM minimizes the differences in the SQL variant used in the database engine • Many frameworks and ORMs don't even require you to know SQL 14 CC BY-NC 4.0 Justin Yost

Slide 15

Slide 15 text

Thanks Questions? • twitter.com/justinyost • github.com/justinyost • justinyost.com • linkedin.com/learning/instructors/justin-yost 15 CC BY-NC 4.0 Justin Yost