Lecture 9 - Stateful Web Flashcards
What does PDO stand for?
PHP Data Object
To configure the PDO to throw an exception any time there’s a failure?
Use the PDO’s setAttribute() method
What is the exec() method used for?
Queries can be sent to a MYSQL database using it.
What does PDO do?
Identifies that the connection to a database has been established
If PHP cannot handle PDO, what will it do?
Throws a php exception
What does the try-catch statement do?
try { do something } catch (Exception type $e){ handle exception }
What does this code do?
This function returns a PHP Data Object (PDO)
IF this PDO code can’t be performed, what happens?
PHP will throw an exception
What does PHP do with a database once a connection is no longer needed?
It automatically disconnects
How do you force PHP to close a connection?
$pdo = null
How do you connect to a database with procedural php?
$variable_name = mysql_connect(hostname, username, password);
How do you close a connection to a mysql database with procedural php?
mysql_close();
What must be do before answering a query?
Establish a database connection
What are the best practices of querying a database?
Establish a connection at the start of the php script
During development, leave clues in case of issues when passing queries to the server. (relevant error messages)
Name three sql queries
DELETE, INSERT, UPDATE
PDO Method: How do we create a table?
$sql = ‘CREATE TABLE jokes(id INT NOT NULLAUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL,
)
$pdo->exec($sql)
PDO Method: How do we update a table?
$sql = ‘UPDATE joke SET jokedate = “2012” WHERE joke text LIKE “%chicken%”;
$affectedRows = $pdo ->exec($sql)
PDO Method: How do we SELECT?
$sql = 'SELECT * FROM tablename'; $result = $pdo->query($sql);
What makes SELECT queries different from other queries?
They produce results
Procedural Method: How do we send SQL queries through php?
$variable_name = mysqli_query(sql statement);
Example:
$variable_name = mysqli_query(SELECT*FROM customers);
What would the result set of a query contain?
A list of all the entries returned from the query
Which PHP functions are needed to connect to the database using procedural php?
mysqli_connect()
mysql_set_chart()
mysqli_select_db()
What does mysqli_connect() do?
Connects to the database
What does mysql_set_chartset() do?
Indicates the character set to be used for
communications between PHP and the DB
Must match the character set used by HTML pages,
DB and its tables
What does mysql_select_db() do?
Indicates the database to be used for queries.
How would you add a new joke from a user, using php?
if (isset($_GET[‘addjoke’]))
{
include ‘form.html.php’;
exit()