PHP-DB-mysqli Flashcards
Connect to datasource (SYNTAX)
$mysqli = new mysqli($hostname, $username, $password, $dbname); if($mysqli->connect_errno) { echo "Connection Failed: " . $mysqli->connect_errno; }
Execute SQL Statement
$result = $mysqli->query(“SELECT * FROM fruits
”);
Process results (SYNTAX)
while($row=$result->fetch_assoc()){ print_r($row); // recursively print out object. echo($row["name"]); //or $row[0]; } $result->free();
Close connection (SYNTAX)
$mysqli->close();
PHP also closes the connection when the script ends
Handling Errors – Most mysqli functions will return an error if it does not success. For example:
Most mysqli functions will return an error if it does not success. For example:
//will return false if the prepare failed. $mysqli -> prepare($sql)
Handling Errors – We can then use ___ statements to check if these functions returned false:
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 }
Multiple SQL statements may be prepared and executed in ____ _____.
Multiple SQL statements may be prepared and executed in one script.
Use PHP programming logic such as ____ and _____ statements to build appropriate SQL as your application requires.
Use PHP programming logic such as loops and if statements to build appropriate SQL as your application requires.
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.
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