sqlite baby steps - small steps for huge success

13
SQLITE BABY STEPS Small Steps for Huge Success

Upload: scott-scoble

Post on 12-Jul-2015

102 views

Category:

Software


0 download

TRANSCRIPT

Page 1: SQLite Baby Steps - Small Steps for Huge Success

SQLITE BABY STEPSSmall Steps for Huge Success

Page 2: SQLite Baby Steps - Small Steps for Huge Success

WTF is PDO?

• PHP Data Object

• An abstraction of your database driver

• An object you can use to talk to just about any

database

Page 3: SQLite Baby Steps - Small Steps for Huge Success

What can I do with PDO?

• Use prepared statements (the main subject of

this presentation)

• Prevent some SQL injection attacks

• Develop using SQLite, but run production with

MySQL or PostgreSQL

Page 4: SQLite Baby Steps - Small Steps for Huge Success

Creating/Connecting

• Instantiate the PDO object with a dsn

• Examples:

• $db = new PDO(‘sqlite::memory:’);

• $db = new

PDO(‘mysql:host=localhost;dbname=test’);

Page 5: SQLite Baby Steps - Small Steps for Huge Success

prepare, bind, execute, fetch

Most queries follow the same pattern:

• prepare() — $stmt = $db->prepare($sql);

• bind() — $stmt->bind(‘:input’, $variable);

• execute() — $stmt->execute();

• fetch() — $stmt->fetchAll(); or $stmt->fetch();

Page 6: SQLite Baby Steps - Small Steps for Huge Success

prepare

• Creates a prepared statement for execution

• Allows for inputs for the query that aren’t

available in code yet

• Great way to separate queries and data model

from implementation and usage

Page 7: SQLite Baby Steps - Small Steps for Huge Success

bind

• Binds the input in a prepared statement to the

pointer of a variable

• Variable does not have to be defined

• Not required

Page 8: SQLite Baby Steps - Small Steps for Huge Success

execute

• Executes the prepared statement on the

database

• Returns true if it passes, false if it fails

• Can take an array of input parameters to be

used with the prepared statement

Page 9: SQLite Baby Steps - Small Steps for Huge Success

fetch(all)

• Fetches the results

• fetch() and fetchAll() are two ways to fetch

results

• Is a cursor and can be iterated over

• Takes a fetch type as an input,

PDO::FETCH_ASSOC is my favorite

Page 10: SQLite Baby Steps - Small Steps for Huge Success

–Bruce Buffer

“It’s time!!!”

Page 11: SQLite Baby Steps - Small Steps for Huge Success

What’re we going to do?

Page 12: SQLite Baby Steps - Small Steps for Huge Success

Questions?

Page 13: SQLite Baby Steps - Small Steps for Huge Success

References

• github project — https://github.com/wscoble/phpug-

pdo101

• twitter — twitter.com/lvcodesmith

• google+ — plus.google.com/+ScottScoble

• LinkedIn — linkedin.com/in/scottscoble