wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Connecting to a Database using PHP (Part 2)

Total questions: 9

Worksheet time: 5mins

Name
Class
Date
1.

The aim of this code is to connect to a Database. Replace the COMMAND_A with the correct command.

a)

mysqli_connect_error

b)

mysqli_query

c)

mysqli_connect

d)

mysqli_fetch_row

2.

The aim of this code is to connect to a Database. Replace the COMMAND_B with the correct command.

a)

mysqli_connect_error

b)

mysqli_query

c)

mysqli_connect

d)

mysqli_fetch_row

3.

You have created the following user for your MySQL database located on "localhost"

Which of the following commands will connect successfully?

a)

mysqli_connect(

"newuser",

"123",

"localhost",

"dbstudents");

b)

mysqli_connect(

"localhost",

"newuser",

"123",

"dbstudents");

c)

mysqli_connect(

"localhost",

"dbstudents",

"newuser",

"123");

d)

mysqli_connect(

"localhost",

"newuser",

"123");

4.

The result of the mysqli_connect() function is very useful.

What can you use $result to do?

a)

Check if result is FALSE:

if ($result == FALSE)

b)

Check if the connection is successful:

if ($result == FALSE)

c)

Execute a SQL query:

mysqli_query

($result, $sql)

d)

Get the number of results in a SQL query:

mysqli_num_rows

($result)

5.

Why is it important to have this line of code?

include_once "1_Connect_DB.php";

a)

It includes the code from another PHP file which connects to the database

b)

It includes the code from another PHP file which creates a new database

c)

It includes the code from another PHP file which enables the code to run error-free

d)

It includes the code from another PHP file which allows

6.

What does the following code do?

$result = mysqli_query($A, $B);

a)

It creates a new query connection to the database to get a result

b)

It makes use of PHP to get a result from the database

c)

It makes use of a query to the database to get a result

d)

It makes use of a database connection to execute an SQL statement

7.

What should be the variable $A?

$result = mysqli_query($A, $B);

*HINT: it was created using the mysqli_connect command

a)

$A should be the method you wish to connect to the database

b)

$A should be the database connection

c)

$A should be the SQL statement that you want to execute

d)

$A should be the results of the SQL query that you want to execute

8.

The table shows the results of the query to the database.

echo mysqli_num_rows($result);

What is the result of the code above?

a)

1

b)

2

c)

3

d)

4

9.

What is the result of the following code?

while ($row = mysqli_fetch_assoc($result)) {

echo $row["Taste"];

echo "<br>";

}

a)

Sour

Sweet

Bitter

b)

1

2

3

c)

Apple

Orange

Bittergourd

d)

1 Apple Sour

2 Orange Sweet

3 Bittergourd Bitter