PHP Set 9 Flashcards
What is PDO?
PHP Data Object: An extension that defines a lightweight, consistent interface for accessing databases in PHP
Why is PDO needed?
There are many database extensions that are similar but don’t provide the same interface
What are the 3 classes provided by PDO?
- PDO class: Represents a connection between PHP and a database server
- PDOStatement class: Represents a prepared statement and an associated result set when the statement is executed
- PDOException class: Represents an error raised by PDO
How many drivers does PDO have for connecting to database systems?
12 sets of drivers to connect to different systems
When should you used prepared APIs vs direct execution APIs when performing SQL queries?
Use prepared when SQL statements may contain user input, can use direct execution when it does not contain user input
What are the 2 parameter methods for PDO prepared statements?
- Named parameters:
$tpl1 = “select slot from meetings
where name = :name and email =
:email”; - Positional parameters:
$tpl2 = “select slot from meetings
where name = ? and email = ?”;
What is the syntax for the PDO prepare method?
PDO::prepare(string $query, array $options=[]): PDOStatement | False
What is returned by the PDO prepare method?
Returns a PDOStatement object on success or FALSE on failure
What is the syntax for the PDO bindValue method?
PDO::bindValue(string|int $param,
mixed $value, int $type =
PDO::PARAM_STR |
PDO::PARAM_INT):bool
Where:
$param is a parameter of the firm
:name for named parameters or the
1-indexed position of the parameter
for positional parameters
$value is the value to bind to the
specified parameter
$type is an explicit data type for the
parameter using the PDO::PARAM_*
constants
What is the syntax for the PDO bindParam method?
bool PDO::bindParam(string|int
$param, mixed &$var, int $type =
PDO::PARAM_STR |
PDO::PARAM_INT, int $maxLength =
0, mixed $driverOptions =
null): bool
Where:
$param is a parameter of the firm
:name for named parameters or the
1-indexed position of the parameter
for positional parameters
$var is the name of the variable to
bind to the parameter
$type is an explicit data type for the
parameter using the PDO::PARAM_*
constants
$maxLength is an optional parameter
for the maximum length of the data
type
$driverOptions is optional
How do you execute a PDO prepared statement?
Using the execute() method when parameters have been set using bindValue() or bindParam()
What is the difference between bindValue() and bindParam()?
bindParam() binds a parameter exclusively to a specified variable name which is bound as a reference
bindValue() binds a value which could be a variable, integer, or string to a parameter
What can you use as PHP debugging tools?
- An online PHP interpreter
- phpdpg the command line debugger
- Xdebug which is an extension for PHP on VScode enabling step by step execution
What are the functions that can be used to dump variables to standard output for debugging?
- var_dump($var): Dumps the variable type and value to stdout
- print_r($var) prints the variable value in human-readable form to stdout
- get_defined_vars() gets all defined variables including built-ins and custom variables
- debug_zval_dump($var) dumps the variable with its reference counts
- debug_print_backtrace() prints a backtrace that shows the current function call chain
- debug_backtrace() gets the backtrace. Handles traces from outside of functions
What are the options that can be specified for switching error reporting level?
These must be set in php.ini
1. error_reporting sets the level of logging
2. display_errors tells PHP if and where to display error messages
3. display_startup_errors should only be used when debugging
4. log_errors and error_log work together to send errors to a specified log file. This should be done in production rather than displaying the logs to users