PHP Set 9 Flashcards

1
Q

What is PDO?

A

PHP Data Object: An extension that defines a lightweight, consistent interface for accessing databases in PHP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is PDO needed?

A

There are many database extensions that are similar but don’t provide the same interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 3 classes provided by PDO?

A
  1. PDO class: Represents a connection between PHP and a database server
  2. PDOStatement class: Represents a prepared statement and an associated result set when the statement is executed
  3. PDOException class: Represents an error raised by PDO
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many drivers does PDO have for connecting to database systems?

A

12 sets of drivers to connect to different systems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should you used prepared APIs vs direct execution APIs when performing SQL queries?

A

Use prepared when SQL statements may contain user input, can use direct execution when it does not contain user input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 2 parameter methods for PDO prepared statements?

A
  1. Named parameters:
    $tpl1 = “select slot from meetings
    where name = :name and email =
    :email”;
  2. Positional parameters:
    $tpl2 = “select slot from meetings
    where name = ? and email = ?”;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax for the PDO prepare method?

A

PDO::prepare(string $query, array $options=[]): PDOStatement | False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is returned by the PDO prepare method?

A

Returns a PDOStatement object on success or FALSE on failure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for the PDO bindValue method?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for the PDO bindParam method?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you execute a PDO prepared statement?

A

Using the execute() method when parameters have been set using bindValue() or bindParam()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between bindValue() and bindParam()?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can you use as PHP debugging tools?

A
  1. An online PHP interpreter
  2. phpdpg the command line debugger
  3. Xdebug which is an extension for PHP on VScode enabling step by step execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the functions that can be used to dump variables to standard output for debugging?

A
  1. var_dump($var): Dumps the variable type and value to stdout
  2. print_r($var) prints the variable value in human-readable form to stdout
  3. get_defined_vars() gets all defined variables including built-ins and custom variables
  4. debug_zval_dump($var) dumps the variable with its reference counts
  5. debug_print_backtrace() prints a backtrace that shows the current function call chain
  6. debug_backtrace() gets the backtrace. Handles traces from outside of functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the options that can be specified for switching error reporting level?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly