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.

1
Q

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.

A

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.

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

Recently, PHP updated the mysql_ functions to _______ functions, where the ‘_’ stands for ‘______’. However, it works only for mysql database-specific functionality.

A

Recently, PHP updated the mysql_ functions to mysqli_ functions, where the ‘i’ stands for ‘improved’. However, it works only for mysql database-specific functionality.

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

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: ?

A

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

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

Individual ______ for each database must be installed for PDO to work properly.

A

Individual drivers for each database must be installed for PDO to work properly.

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

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.

A

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.

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

Never use ________ functions. Use only ______ or _____ functions. We will use PDO in this course.

A

Never use mysql_ functions. Use only mysqli_ or PDO functions. We will use PDO in this course.

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

The general process for working with databases:

1.
2.
3.
4.

A

The general process for working with databases:

  1. Connect to datasource
  2. Prepare and execute SQL statement
  3. Process the results
  4. Close database
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe this statement:

$dsn = “mysql:host=
localhost;dbname=
my_database;charset=utf8mb4”;

A
  1. This is the datasource string
  2. It uses the mysql driver
  3. Connecting to localhost (means that the database is running on the same server the script lives
  4. my_database represents the database name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe this statement:

$pdo = new PDO($dsn, $dbusername, $dbpassword);

A
  1. Creates a new PDO object
  2. Connects to the datasource using the credentials provided.
  3. $pdo is a reference to the new PDO object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe this statement:

$stmt = $pdo->prepare(“SELECT name, color FROM fruits WHERE name = ‘apple’”);

A
  1. The prepare method (a method of the $pdo object) accepts the SQL statement to be used
  2. The prepare method returns a statement object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe this statement:

$stmt->execute();

A

The execute method executes the statement.

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

When the statement gets executed, use the “____” method to read the returned data.

A

When the statement gets executed, use the “fetch” method to read the returned data.

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

Describe this statement:

while($row = $stmt->fetch()) {
print_r($row); // recursively print out object.
echo($row[“name”]); //or $row[0];
}

A

The fetch() method can take a variety of parameters that returns the data in different ways.

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

PHP also _____ the connection when the script ends

A

PHP also closes the connection when the script ends

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

PDO comes with ____ handling.

\_\_\_\_\_\_{
    // PDO statement go here
} catch(PDOException $e) {   
    echo 'Error: ' . $e->getMessage(); 
}
A

PDO comes with error handling.

try {
    // PDO statement go here
} catch(PDOException $e) {   
    echo 'Error: ' . $e->getMessage(); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Helpful Tips:

A
  1. Multiple SQL statements may be prepared and executed in one script.
  2. Use PHP programming logic such as loops and if statements to build appropriate SQL as your application requires.
  3. 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