PDO Database Connections

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
WEB SECURITY WEEK 2 Computer Security Group University of Texas at Dallas.
Class06 Conditional Statements MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/10/2015 © 2014,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
Form Data (part 1) MIS 3502, Fall 2015 Brad Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015.
Class02 Introduction to web development concepts MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 1/14/2016.
Sessions and cookies MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 4/12/2016.
PDO Database Connections MIS 3501, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 3/8/2016.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Web Systems & Technologies
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
PHP Built-In Functions
Brad N Greenwood, PhD MBA
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Introduction to Dynamic Web Programming
Organize your code with MVC
Introduction to web development concepts
Introduction to Web Development (Part 2)
Web Database Programming Using PHP
DBW - PHP DBW2017.
PHP: includes MIS 3501 Jeremy Shafer Department of MIS
Sessions and cookies (part 2)
Form Data (part 1) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Class07 PHP: loops and includes
PDO Database Connections
How to get data from a form
Intro to PHP & Variables
PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer
PDO Database Connections: Getting data out of the database
Introduction to JavaScript
PDO Database Connections: Getting data out of the database
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
A second look at JavaScript
Functions BIS1523 – Lecture 17.
PDO Database Connections
Organize your code with MVC
Introduction to relational databases and MySQL
Sessions and cookies (part 1)
PDO Database Connections
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Web DB Programming: PHP
Class05 How to get data from a form
MySQL Backup, Transfer and Restore
PDO Revisited MIS 3502 Jeremy Shafer Department of MIS
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
PHP: Database connection
Class11 Introduction to relational databases and MySQL
Tutorial 6 PHP & MySQL Li Xu
Form Data (part 1) MIS3501 Jeremy Shafer Department of MIS
Introduction to JavaScript
An introduction to jQuery
MVC – Model View Controller
PDO and Arrays MIS 3502 Jeremy Shafer Department of MIS
PHP Forms and Databases.
An introduction to jQuery
PHP-II.
Presentation transcript:

PDO Database Connections MIS 3501, Fall 2015 Jeremy Shafer Department of MIS Fox School of Business Temple University 10/13/2015

Course Overview We are here. To do: MySQL Weeks 6 & 7 HTML & CSS Weeks 1 & 2 PHP Weeks 3 – 5 PDO Week 8 & 9 To do: Organize your code with MVC (week 10) Work with forms (week 11) Use cookies and sessions (week 12)

? PHP and MySQL (One System) Apache Web Service MySQL PHP Interpreter Databases PHP Interpreter

PHP and MySQL (2) (One System) Apache Web Service MySQL statement PDO PDO Exception MySQL Databases PHP Interpreter (objects)

What’s an object? An object is a data type, like an integer, string or Boolean data type. Only it is very special. It can: Have values (we call them properties) Perform actions (we call them methods) Consequently, objects are very, very flexible way of representing different ideas. PHP defines a number of object classes (that is, kinds of objects) for you. PDO is one of those predefined classes of objects.

A (slightly) more concrete example: Suppose we have a video game where there are a number of spaceships. The programmer of the video game would define each spaceship as an object. The instances of the spaceship object would have properties like size, color, and type. The instances of the spaceship object would also have methods like move left, move right, shoot, explode… etc.

Why are you telling me all this? We’re discussing this object oriented concept so that the syntax of the PDO statements in PHP will make sense. We use the “new” command to create a new instance of an object, and the arrow shaped object operator “->” to reference attributes and methods. For example: $spaceship1 = new SpaceShip(); $spaceship1->color = “Green”; $spaceship1->size = “small”; $spaceship1->jumpToCoordinates(0,0); $spaceship1->moveLeft(); $spaceship1->shoot(); $spaceship1->destroy();

Working with a database connection So, when we decide to use a database connection, we use PDO and statement objects, and their methods, to communicate with the database. $db = new PDO("mysql:host=localhost;dbname=class15", "class15", "password"); $statement = $db->prepare("insert into names(name) values ('Bob')"); $statement->execute(); $statement->closeCursor();

Opening a PDO/MySQL connection You can open a connection to a MySQL database server creating a new PDO() object. Assign the return value from the new PDO() command to a variable. Then use that variable to represent the database in your script. The PDO object takes three parameters that indicate what database to connect to and how. The connection string, the username, and the password. $db = new PDO("mysql:host=localhost;dbname=class15","class15", "password");

Opening a PDO/MySQL Connection (2) $db = new PDO("mysql:host=localhost;dbname=class15","class15", "password"); #1 The connection string argument specifies tells PDO what sort of database to connect to, and where it is. These strings are also called dsn strings (dsn is short for “data source name”) The connection string can indicate any number of settings necessary to connect. The above example shows a very minimalistic connections string where the type of database is indicated (mysql) followed by a host and database name.

Opening a PDO/MySQL Connection (2) $db = new PDO("mysql:host=localhost;dbname=class15","class15", "password"); #1 #2 #3 The connections string argument specifies tells PDO what sort of database to connect to, and where it is. The user argument specifies a MySQL account name The password argument specifies a MySQL account password

Creating a PDO statement Once you have a database connection, you can use the prepare() method to create a statement. The prepare method takes a string of SQL text. After you have prepared the statement, use the execute() method to send it to the database. Finally, tell the system to clean things up by closing the statement with the closeCursor() method. (See pdo1.php)

Passing values to the database PDO gives you a way to send SQL statements to the database that contain variables. In order to do that, use the bindValue() method of the statement object. This method will allow you to substitute a placeholder in the SQL string with a value. (See pdo2.php)

Catching connection errors Connections aren’t always successful. We can use try / catch to address this possibility. If the connection fails, then the catch block of code will be executed. We specify yet another object (the PDOException object) in the catch block. We can then use the getMessage() method of the exception object to determine what went wrong. (See pdo3.php and pdo4.php)

Give it a try… Following the example files provided, can you create a form, with a single input tag, that populates the names table? You don’t need to upload or send this to me. You should just try to do this.