PHP-DB-mysqli Flashcards

1
Q

Connect to datasource (SYNTAX)

A
$mysqli = new mysqli($hostname, $username, $password, $dbname);
if($mysqli->connect_errno) {
  echo "Connection Failed: " . $mysqli->connect_errno;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Execute SQL Statement

A

$result = $mysqli->query(“SELECT * FROM fruits”);

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

Process results (SYNTAX)

A
while($row=$result->fetch_assoc()){
    print_r($row); // recursively print out object.
    echo($row["name"]); //or $row[0];
}
$result->free();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Close connection (SYNTAX)

A

$mysqli->close();

PHP also closes the connection when the script ends

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

Handling Errors – Most mysqli functions will return an error if it does not success. For example:

A

Most mysqli functions will return an error if it does not success. For example:

//will return false if the prepare failed.
$mysqli -> prepare($sql)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Handling Errors – We can then use ___ statements to check if these functions returned false:

A

We can then use if statements to check if these functions returned false:

if($stmt = $mysqli -> prepare($sql)){
	//continue with bind and execute
}else{
	//handle error
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Multiple SQL statements may be prepared and executed in ____ _____.

A

Multiple SQL statements may be prepared and executed in one script.

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

Use PHP programming logic such as ____ and _____ statements to build appropriate SQL as your application requires.

A

Use PHP programming logic such as loops and if statements to build appropriate SQL as your application requires.

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

Use the “_________” method on the PDO object to get the id of the record that was last inserted.

This is useful when inserted records need to be referenced from another table when operating on multiple tables.

A

Use the “lastInsertId” method on the PDO object to get the id of the record that was last inserted.

This is useful when inserted records need to be referenced from another table when operating on multiple tables

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