Chapter 4- PHP with mySQL
What method of the PDO class do you use to execute a SELECT statement?
$PDO_object -> query($select_statement) ----------------------------- this returns a PDOstatement object that contains the result set. If no result set is returned, this method returns a FALSE value. ----------------------------------
How to create the DSN for a mySQL database titled my_guitar_shop1?
$dsn = 'mysql:host=localhost;dbname=my_guitar_shop1';
How to connect to a mySQL database?
$dsn = 'mysql:host=localhost;dbname=squilliam;' $username = 'mgs_user'; $password = 'pas55word' $db= new PDO($dsn, $username, $password);
Syntax for executing a method of any object?
$ob->methodName(argumentList) ------------------------------- You code the name of object, followed by ->, followed by the name of the method, followed by a set of parentheses.
A query method with the SELECT statement coded as the argument
$products = $db-> query('SELECT * FROM products');
A query method with the SELECT statement coded in a variable
$query = 'SELECT * FROM products WHERE categoryID = 1 ORDER BY productID'; $products = $db->query($query); //products contains the result set
Execute a SQL statement with no parameters: (using prepare, bindValue, execute)
$query = 'SELECT * from products WHERE categoryID = :category_id'; $statement = $db->prepare($query); $statement->bindValue(':category_id', $category_id); $statement->execute();
Execute a SQL statement with no parameters: (using prepare and execute)
$query = 'SELECT * from products'; $statement = $db -> prepare($query); $statement -> execute(); -------------------------- prepare to be executed
What is the method of the PDO class for preparing a SQL statement?
$statement1 = $db -> prepare($sql_statement); ------------------------------ Prepares the specified SQL statement for execution and returns a PDOStatement object. Within the SQL statement, you can use a colon (:) to identify named parameters.
What method gets assigned to a new variable out of these three: 1. prepare($query) 2. bindValue(param, value); 3. execute()
1. prepare($query); aka $statement = db -> prepare($query); $statement -> bindValue(param, value); $statement -> execute();
How many arguments to create a PDO object that connects to a MySQL databse from the PDO class?
3. ----------------------- DSN (data source name), username, and password.
What does bindValue($param, $value) do?
Binds the specified value to the specified parameter in the prepared statement. ----------------------- Looks like you use this when you have params (aka WHERE)...i think bindValue is used when you are getting input from user....
What does execute do?
Executes the prepared statement.
closeCursor()
Frees the connection to the server so other SQL statements may be issued.
What is a DSN?
Identifies the host computer and the database to be used.
What class is used for errors thrown by the PDO library?
PDOException class
FETCH GIVES YOU THE ______ IN AN ARRAY, YOU ACCESS THE RESULTS BY GETTING THE _____ YOU WANT AS THE _____.
ROW, COLUMNS , INDEX
What are the two methods of the PDOStatement class for executing a statement?
bindValue($param, $value) and execute()
How to handle exceptions?
code a try/catch statement. 1.) code a try block around any PHP statements that might throw an exception. 2.) Code a catch block that catches the exception (known as exception handling).
Object that contains information about an error that has occurred
exception
Which method to use to execute INSERT, UPDATE, and DELETE statements?
exec($sql_statement) --------------------------- executes the specified SQL statement and returns the number of affected rows. If no rows were affected, the method returns zero.
When the query method of a database is executed, it returns the result set in a PDOStatement object. What do you use to get the data from the result set for that object?
foreach $query = 'SELECT productCode, productName, listPrice FROM products WHERE categoryID = 1;' $products = $db -> query($query);
All Exception objects provide a _______ method that lets you get the error message.
getMessage()
The DSN for a mySQL connection specifies?
host computer for the database and the name of the database.
Syntax to create the DSN for a MySQL db?
mysql:host=localhost;dbname=database_name;
How to create an object from any class?
new ClassName(arguments) ------------------------------ code the new keyword, followed by the name of the class, followed by a set of parentheses. Within the parentheses, you code the arguments that are required by the class, separating multiple arguments with commas.
How to create a database object from the PDO class?
new PDO($dsn, $username, $password)
fetch()
returns an array for the first row (or next) in the result set. If no array is available, returns a FALSE value.