Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN 6710 - Section A – TR 9:30-10:45 CRN 10570 – Section B – TR 5:30-6:45.

Similar presentations


Presentation on theme: "Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN 6710 - Section A – TR 9:30-10:45 CRN 10570 – Section B – TR 5:30-6:45."— Presentation transcript:

1 Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN 6710 - Section A – TR 9:30-10:45 CRN 10570 – Section B – TR 5:30-6:45

2 PHP Data Object (PDO) PDO is the currently preferred way to access a database The object can be configured to connect to several different database types, allowing you to learn one set of functions for multiple databases

3 PDO Connection $db = new PDO($connectionString, $username, $password) $connectionString: mysql:host=123.123.123.123:123;dbname=database; Once the object is created, you can invoke several methods to invoke SQL queries

4 PDO Basic Select $query = $db->query(“SELECT * FROM table”); This creates a query object, but does not actually run the query $results = $query->fetch(…);//return next row ->fetchAll(…);//return all rows Inside of the fetch command, we want to specify how the data should be returned PDO::FETCH_ASSOC PDO::FETCH_NUM PDO::FETCH_BOTH

5 PDO Basic Select Often times when using fetch(), we will use a while loop while($row = $query->fetch(PDO::FETCH_ASSOC)) And we may end the while loop early, meaning we don’t actually retrieve all the rows, saving some processing time If we need to know the number of results, or know we need to get everything, we can use fetchAll() foreach($query->fetchAll(PDO::FETCH_ASSOC) AS $key => $val)

6 PDO Security Using the query method can be a simple way to get your query executed, but if you are using any data the user provided to build your query, there is a chance of SQL Injection To avoid this, we use the prepare() method, with bindParam() methods The bindParam() method will ensure that the data you are adding to the query is properly sanitized before the SQL is executed

7 PDO Prepare The prepare() method is also helpful in building an object that contains a query we want to run multiple times, for instance, when inserting several rows at a time $select = $db->prepare(“SELECT * FROM table WHERE ID = ?”); $select->bindParam(1, $id); $id = 5; $select->execute(); $results = $select->fetchAll();

8 PDO Prepare $select = $db->prepare(“SELECT * FROM table WHERE ID = :id”); $select->bindParam(“:id”, $id); $id = 5; $select->execute(); $results = $select->fetchAll(); $id = 6; $select->execute(); $results = $select->fetchAll();

9 PDO Insert Insert/Update/Delete commands are all executed the same way as the select examples we did, using query() or prepare() There are a few additional methods that are helpful to us with these items: $db->lastInsertId(); $db->errorCode(); $db->errorInfo(); $update->rowCount();//doesn’t work with select queries errorCode and errorInfo can also be run off of prepare() objects

10 PDO Practice Select rows from a table Insert a new row into a table Update an existing row with new data Delete an existing row

11 Lab 3 - PHP Due: Sept 24 th Create a PHP page with a simple web form Include at least: Two text fields ( ) One drop down ( ) One radio button group with at least two radio buttons ( //same name attribute will cause them to group together) A submit button

12 Lab 3 - PHP Set up PHP code to validate that all fields had values entered Create styled error messages if fields were not completed (for example, red text saying which field was not completed) Output the values entered on the page

13 Lab 4 - MySQL Due: Oct 1 Create a simple web form Create a MySQL DB and Table to store the submissions from the form Set up the PHP code to write the submissions to the table Use PHP to create a table below the form with all of the data currently in the table (when new data is submitted to the form, another table row should appear with that data)

14 Next Time Model, View, Controller (MVC) using CodeIgniter We will be working with this topic for two weeks CodeIgniter is a PHP framework, so we will be spending the next two weeks working with more PHP and MySQL


Download ppt "Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN 6710 - Section A – TR 9:30-10:45 CRN 10570 – Section B – TR 5:30-6:45."

Similar presentations


Ads by Google