PHP DB Flashcards
At the end of this class, students should be able to: Connect to a datasource Prepare and execute SQL statements using PDO and/or mysqli Process query results Produce a web application that uses static and dynamic pages employing database access on a server.
MySQL is just one of many databases that PHP can connect to. Other databases include PostGre SQL, MSSQL, IBM, ODBC, Oracle and so on. In the past, PHP provided different _________-specific functions for different databases.
MySQL is just one of many databases that PHP can connect to. Other databases include PostGre SQL, MSSQL, IBM, ODBC, Oracle and so on. In the past, PHP provided different database-specific functions for different databases.
Recently, PHP updated the mysql_ functions to _______ functions, where the ‘_’ stands for ‘______’. However, it works only for mysql database-specific functionality.
Recently, PHP updated the mysql_ functions to mysqli_ functions, where the ‘i’ stands for ‘improved’. However, it works only for mysql database-specific functionality.
The new PDO functions allow developers to use _____ set of functions to connect to and work with different types of _______.
This is known as __________ or PDO for short. PDO with the Database drivers provide a “______-______ abstraction layer” between the database and PHP code (PDO) and the database.
Diagram: ?
The new PDO functions allow developers to use one set of functions to connect to and work with different types of databases.
This is known as PHP Data Objects or PDO for short. PDO with the Database drivers provide a “data-access abstraction layer” between the database and PHP code (PDO) and the database.
Diagram: PDO | PHP DB DRIVER DATABASE
Individual ______ for each database must be installed for PDO to work properly.
Individual drivers for each database must be installed for PDO to work properly.
Until now, the native functions we used were procedural in nature. PDO is a relatively new set of functionality available to PHP and it uses a more _____ _____ approach to PHP development. This reflects the development of PHP in general.
Until now, the native functions we used were procedural in nature. PDO is a relatively new set of functionality available to PHP and it uses a more Object Oriented approach to PHP development. This reflects the development of PHP in general.
Never use ________ functions. Use only ______ or _____ functions. We will use PDO in this course.
Never use mysql_ functions. Use only mysqli_ or PDO functions. We will use PDO in this course.
The general process for working with databases:
1.
2.
3.
4.
The general process for working with databases:
- Connect to datasource
- Prepare and execute SQL statement
- Process the results
- Close database
Describe this statement:
$dsn = “mysql:host=
localhost;dbname=
my_database;charset=utf8mb4”;
- This is the datasource string
- It uses the mysql driver
- Connecting to localhost (means that the database is running on the same server the script lives
- my_database represents the database name
Describe this statement:
$pdo = new PDO($dsn, $dbusername, $dbpassword);
- Creates a new PDO object
- Connects to the datasource using the credentials provided.
- $pdo is a reference to the new PDO object
Describe this statement:
$stmt = $pdo->prepare(“SELECT name, color FROM fruits WHERE name = ‘apple’”);
- The prepare method (a method of the $pdo object) accepts the SQL statement to be used
- The prepare method returns a statement object.
Describe this statement:
$stmt->execute();
The execute method executes the statement.
When the statement gets executed, use the “____” method to read the returned data.
When the statement gets executed, use the “fetch” method to read the returned data.
Describe this statement:
while($row = $stmt->fetch()) {
print_r($row); // recursively print out object.
echo($row[“name”]); //or $row[0];
}
The fetch() method can take a variety of parameters that returns the data in different ways.
PHP also _____ the connection when the script ends
PHP also closes the connection when the script ends
PDO comes with ____ handling.
\_\_\_\_\_\_{ // PDO statement go here } catch(PDOException $e) { echo 'Error: ' . $e->getMessage(); }
PDO comes with error handling.
try { // PDO statement go here } catch(PDOException $e) { echo 'Error: ' . $e->getMessage(); }