Presentation is loading. Please wait.

Presentation is loading. Please wait.

PDO Database Connections: Getting data out of the database

Similar presentations


Presentation on theme: "PDO Database Connections: Getting data out of the database"— Presentation transcript:

1 PDO Database Connections: Getting data out of the database
MIS 3501 Jeremy Shafer Department of MIS Fox School of Business Temple University

2 Course Overview We are here... To do: MySQL
2 Weeks We are here... HTML & CSS 2 Weeks PHP 3 Weeks PDO 2 weeks To do: Organize your code with MVC (1 week) Work with forms (1 week) Use cookies and sessions (1 week)

3 Recall from last time... (One System) Apache Web Service MySQL
statement PDO PDO Exception MySQL Databases PHP Interpreter (objects)

4 Working with a database connection
We use PDO and statement objects, and their methods, to communicate with the database. $db = new PDO("mysql:host=localhost;dbname=exercisePDO1", "exercisePDO1", "password"); $statement = $db->prepare("insert into names(name) values ('Bob')"); $statement->execute(); $statement->closeCursor(); Here we were able to prepare and execute an INSERT statement. UPDATE and DELETE statements will work the same way. So….

5 What happens when we send a SELECT statement to the database?
The execute() method still returns TRUE on success and FALSE on failure. But now we need a method that will fetch a record from those results: fetch(); Fetch is a method of the statement object. Fetch will return an associative array. Here we put the results of that fetch into a variable: $card = $statement->fetch();

6 What’s an array? For the moment – let’s just say that an array is a way of grouping a bunch of simpler, smaller variables, under a single variable name. You’ve already seen and used two arrays: $_GET and $_POST. Now, the big news is that you can define your own arrays and (more importantly) when you pull a row out of a database table into PHP it its stored in an array.

7 An analogy …

8 What does fetch() do? The result is multiple records. We need to treat them one at a time. The statement object holds these results. This select statements is sent to the database: SELECT custid, firstname, lastname FROM tblContacts WHERE statecode = ‘PA’ ORDER BY lastname, firstname; custid – 1 firstname – Adam lastname – Ames

9 What does fetch() do? Here are all the records that remain in the statement object. We called fetch() and the first record was retrieved. custid – 1 firstname – Adam lastname – Ames custid – 2 firstname – Ben lastname -- Burns

10 What does fetch() do? Call fetch() again and the next record is retrieved. custid – 3 firstname – Carl lastname – Clarke custid – 2 firstname – Ben lastname -- Burns

11 What does fetch() do? Call fetch() again and the next record is retrieved. custid – 4 firstname – Dan lastname – Dent custid – 3 firstname – Carl lastname – Clarke

12 What does fetch() do? Call fetch() again and the next record is retrieved. custid – 5 firstname – Ed lastname - Elison custid – 4 firstname – Dan lastname – Dent

13 What does fetch() do? Call fetch() again and the next record is retrieved. custid – 6 firstname – Fred lastname - Franke custid – 5 firstname – Ed lastname - Elison

14 Uh oh… there’s nothing left in the statement object.
What does fetch() do? Call fetch() again and the next record is retrieved. Uh oh… there’s nothing left in the statement object. custid – 6 firstname – Fred lastname - Franke So what is the value going to be the next time the fetch() method is called? It can’t be an array (there’s no data)… so it is…

15 FALSE What does fetch() do?
The fetch method, of the statement object, returns a value of false when there is no more data to be retrieved. So here we have an operation that pulls data out of a database table. We perform the same action, over and over again, until there’s no more data left retrieve. What sort of PHP control structure should we use to accomplish this repetitive task?

16 Associative array In the prior example, each “card” is something called an associative array. The associative array is very much like the GET and POST arrays we have already seen. So... after the first fetch… echo $card['custid']; // would echo 1 echo $card['firstname']; // would echo Adam echo $card['lastname']; // would echo Ames How did we know what to put in here? These are the column names from the original select statement: SELECT custid, firstname, lastname FROM tblContacts WHERE statecode = ‘PA’ ORDER BY lastname, firstname; (See pdo5.php )

17 But we can do a bit better…

18 What does fetchall() do?
The fetchall() method will retrieve the entire array in one call. Then you can loop through the array with something called a foreach loop. The overall effect is the same, but the mechanics in the background are different. $allthecards = $statement->fetchall(); foreach ($allthecards as $card) { echo "<p>"; echo $card["firstname"]; echo "</p>"; } (See pdo6.php )


Download ppt "PDO Database Connections: Getting data out of the database"

Similar presentations


Ads by Google