PHP and MySQL Flashcards

1
Q

Q: What is MySQL?

A

A: A relational database management system (RDBMS) that stores and manages data.

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

Q: How does PHP interact with MySQL?

A

A: PHP uses extensions like mysqli or PDO to interact with MySQL databases.

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

Q: What is the difference between mysqli and PDO?

A

A: mysqli is specific to MySQL, while PDO supports multiple databases.

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

Q: What is the default MySQL port?

A

A: Port 3306.

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

Q: What PHP function is used to connect to a MySQL database using mysqli?

A

A: mysqli_connect().

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

Q: How do you create a MySQL database connection with mysqli?

A

$conn = mysqli_connect(“host”, “username”, “password”, “database”);

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

Q: What function checks if a database connection was successful?

A

A: mysqli_connect_errno().

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

Q: How do you close a MySQL connection in PHP?

A

A: Use mysqli_close($conn);.

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

Q: What exception is thrown for connection errors in PDO?

A

A: PDOException.

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

Q: How do you set up a database connection using PDO?

A

$pdo = new PDO(“mysql:host=host;dbname=database”, “username”, “password”);

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

Q: What function is used to execute a query in mysqli?

A

A: mysqli_query().

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

Q: How do you execute a query using PDO?

A

A: Use the query() or prepare() method.

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

Q: What function fetches data as an associative array in mysqli?

A

A: mysqli_fetch_assoc().

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

Q: What method fetches data in PDO?

A

A: Use fetch() or fetchAll().

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

Q: How do you handle errors during a query in mysqli?

A

A: Check mysqli_error($conn).

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

Q: What are prepared statements in PHP?

A

A: A way to execute SQL queries with placeholders to prevent SQL injection.

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

Q: How do you create a prepared statement in mysqli?

A

A: Use mysqli_prepare().

18
Q

Q: How do you bind parameters in a mysqli prepared statement?

A

A: Use mysqli_stmt_bind_param().

19
Q

Q: What function executes a prepared statement in mysqli?

A

A: mysqli_stmt_execute().

20
Q

Q: How do you use placeholders in PDO?

A

A: Use named (:name) or positional (?) placeholders in the query.

21
Q

Q: What function fetches all rows as an array in mysqli?

A

A: mysqli_fetch_all().

22
Q

Q: What method retrieves a single row in PDO?

A

A: fetch(PDO::FETCH_ASSOC).

23
Q

Q: How do you loop through rows in mysqli?

A

while ($row = mysqli_fetch_assoc($result)) {
// Process $row
}

24
Q

Q: How do you count the number of rows in a result set in mysqli?

A

A: Use mysqli_num_rows().

25
Q

Q: How do you count rows in a result set in PDO?

A

A: Use the rowCount() method.

26
Q

Q: How do you insert data into a MySQL database using PHP?

A

$query = “INSERT INTO table (column1, column2) VALUES (‘value1’, ‘value2’)”;
mysqli_query($conn, $query);

27
Q

Q: How do you update data in a MySQL database using PHP?

A

$query = “UPDATE table SET column1 = ‘value’ WHERE id = 1”;
mysqli_query($conn, $query);

28
Q

Q: How do you delete data from a MySQL database using PHP?

A

$query = “DELETE FROM table WHERE id = 1”;
mysqli_query($conn, $query);

29
Q

Q: How do you select data from a database using PHP?

A

$query = “SELECT * FROM table”;
$result = mysqli_query($conn, $query);

30
Q

Q: How do you handle SQL injection in PHP?

A

A: Use prepared statements or the mysqli_real_escape_string() function.

31
Q

Q: What is a database transaction?

A

A: A sequence of database operations executed as a single unit.

32
Q

Q: How do you start a transaction in mysqli?

A

A: Use mysqli_begin_transaction().

33
Q

Q: How do you commit a transaction in mysqli?

A

A: Use mysqli_commit().

34
Q

Q: How do you roll back a transaction in mysqli?

A

A: Use mysqli_rollback().

35
Q

Q: How do you manage transactions in PDO?

A

A: Use beginTransaction(), commit(), and rollBack() methods.

36
Q

Q: How do you check for query errors in mysqli?

A

A: Use mysqli_error($conn).

37
Q

Q: How do you enable exceptions for errors in PDO?

A

A: Set the error mode to exception:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

38
Q

Q: What function provides error information for prepared statements in mysqli?

A

A: mysqli_stmt_error().

39
Q

Q: What is the purpose of mysqli_report()?

A

A: To set reporting for MySQL errors and warnings.

40
Q

Q: How do you log database errors in PHP?

A

A: Use error_log() to write errors to a file or system log.