Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP and Databases

PHP and Databases

Basic ability to connect to databases with PHP.

Accompanying code here: https://github.com/justinyost/basic-php-and-databases

Justin Yost

October 01, 2020
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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