Geek Austin PHP Class - Session 4

14
Beginning PHP Session #4 November 30, 2010 Josh Butts

description

 

Transcript of Geek Austin PHP Class - Session 4

Page 1: Geek Austin PHP Class - Session 4

Beginning PHPSession #4

November 30, 2010

Josh Butts

Page 2: Geek Austin PHP Class - Session 4

Agenda

•More sessions (briefly)

•Database access, SQL

•Creating a real login system for Vanity

Page 3: Geek Austin PHP Class - Session 4

So, Databases...

•This is what you’re really here for

•PHP & Databases == Peanut Butter & Jelly

•Databases will be your go to method for storing any sort of data for your application

Page 4: Geek Austin PHP Class - Session 4

SQL

•SQL = Structured Query Language

•SQL is how we get data into and out of databases

•Not really a programming language

•Sort of standardized

Page 5: Geek Austin PHP Class - Session 4

Queries

•We write queries as plain text (strings in PHP)

•We execute queries against a database

•The database returns a Result Set

Page 6: Geek Austin PHP Class - Session 4

Result Sets

•Rows and columns of data that come out of a database

•We loop over these to use them in PHP

•We map these to associative arrays access our data

Page 7: Geek Austin PHP Class - Session 4

Database Tables

•One database has many tables

•Think Excel workbook with many sheets

•Tables have a schema

•Named columns

•Definition for what kind of data each column can store

Page 8: Geek Austin PHP Class - Session 4

Columns

•Mainly 2 types

•Various types of strings

•Various types of numbers

•Definition is essentially “is it a string or a number, and how big will it be”

Page 9: Geek Austin PHP Class - Session 4

Primary Keys

•The “id” column

•Usually an integer

•Usually will automatically count up for you as you insert new rows

•Must be unique values

Page 10: Geek Austin PHP Class - Session 4

Disclaimer

•For ease of use, we’re using SQLite, which is slightly different than MySQL

•Some SQL for SQLite may not work on MySQL without a few tweaks

•We’ll cover MySQL later

Page 11: Geek Austin PHP Class - Session 4

SELECT queries

•Gets data out of your database

•Specifies:

•What columns you want

•What table to look at

•What conditions to satisfy

Page 12: Geek Austin PHP Class - Session 4

INSERT queries

•Write new data to your database

•Specifies:

•The table to write to

•Columns to be written to

•Data for each column listed

Page 13: Geek Austin PHP Class - Session 4

UPDATE queries

•Changes existing data in your database

•Specifies:

•Table to write to

•columns and values to update

•How to find the rows to update (usually by primary key or “id”)

Page 14: Geek Austin PHP Class - Session 4

DELETE queries

•Delete data from the database

•Specifies:

•Which table to delete from

•Conditions for which rows should be deleted