TN Online TestSamacheer Kalvi practice

Connecting PHP and MYSQL: One Mark Questions, Book Back Answers and Notes

12th Standard · Computer Applications · #9

This chapter explains how a PHP script talks to a MySQL database. It covers setting up the server software, opening a connection with mysqli_connect(), checking for connection errors, running SQL with mysqli_query(), reading rows from the result, and closing the connection with mysqli_close().

Study this chapter

Asked in previous board exams

5 questions from this chapter came in 5 past public exams. Open any of them to see the answer and explanation.

  1. March 2026
    Which version of PHP supports MySQLi functions?
    See answer in the solved paper →
  2. March 2024
    Which of the following is the correct function for closing connection in PHP ?
    See answer in the solved paper →
  3. May 2022
    SQL can be expanded as ________.
    See answer in the solved paper →
  4. Supplementary 2021
    How many parameter are required for MySQLi close function in PHP ?
    See answer in the solved paper →
  5. March 2020
    ________ is a request for data or information from a database table or combination of tables.
    See answer in the solved paper →

About Connecting PHP and MYSQL

Medium ~45 min study

Almost every useful website stores data: student records, orders, user accounts. That data lives in a database such as MySQL, while the page itself is produced by PHP. On its own, PHP cannot see the tables, and MySQL cannot build a web page. This chapter shows how the two are joined so that a script can read records from a table and display them.

PHP does this through the MySQLi extension, a set of built-in functions whose names start with mysqli_. You use one function to open the connection with the server name, user name, password and database name, another to send an SQL statement, others to read the rows that come back, and one more to close the connection. Once you see the order in which these calls happen, the example programs in the textbook become easy to follow.

Exam questions from this chapter focus on the functions themselves: what arguments mysqli_connect() takes and in what order, what mysqli_query() and mysqli_close() need, and how to report a failed connection. Book back MCQs often show several versions of a function call and ask which one has the correct syntax, so learn each call with its arguments exactly as the textbook writes them.

What you'll learn

  • Describe the steps a PHP script follows to work with a MySQL database.
  • Open a database connection with mysqli_connect() using the correct arguments in the correct order.
  • Check for a failed connection and print the error message.
  • Run SQL statements from PHP using mysqli_query().
  • Display the rows of a query result on a web page using mysqli_fetch_array() in a loop.
  • Close a database connection with mysqli_close().

Before you start

  • PHP variables, data types and basic syntax.
  • SQL statements such as SELECT, INSERT, UPDATE and DELETE.
  • What localhost means and what a database user name and password are.

Topics covered in this chapter

MySQLi Extension The improved set of PHP functions, introduced in PHP 5.0.0, that lets a script connect to and work with a MySQL database.
Database Connection The link between a PHP script and the MySQL server, opened using the server name, user name, password and database name.
mysqli_connect() Opens the connection; written in the textbook as mysqli_connect("Server Name", "User Name", "Password", "DB Name") and returns a connection object.
Checking Connection Errors Using mysqli_connect_errno() to detect a failed connection and mysqli_connect_error() to print the reason before stopping the script.
mysqli_query() Sends an SQL statement to the server; takes the connection object first and the SQL query string second.
Fetching Records Reading the rows of a SELECT result one by one with mysqli_fetch_array() in a while loop, and counting them with mysqli_num_rows().
Releasing Server Resources Closing connections when the work is done so the MySQL server does not run out of available connections on a busy site.
mysqli_close() Closes an open connection; takes a single argument, the connection object returned by mysqli_connect().

Connecting PHP and MYSQL explained

Connecting a PHP Script to a MySQL Database

Why PHP Needs a Database Connection

A database-driven page follows a simple pattern. The PHP script connects to the MySQL server, sends an SQL statement, receives the result and turns it into HTML for the browser. PHP provides the MySQLi extension for this, where the i stands for improved. It was introduced in PHP 5.0.0 to work with newer MySQL servers. With these functions a script can insert a registration record, update a student's marks or list all the rows of a table. To practise on your own computer you need a web server, PHP and MySQL installed together, which packages such as XAMPP or WAMP provide.

Opening the Connection and Checking for Errors

The connection is opened with mysqli_connect(). The textbook syntax is mysqli_connect("Server Name", "User Name", "Password", "DB Name");, for example $con = mysqli_connect("localhost", "my_user", "my_password", "Student_DB");. For a database on the same machine, the server name is usually localhost. The function returns a connection object, which you store in a variable and pass to the other functions. If the connection fails, the script should say so and stop, instead of trying to run queries on a connection that does not exist. The textbook checks this with if (mysqli_connect_errno()) and prints the reason using mysqli_connect_error().

Running Queries and Reading the Result

Once connected, SQL statements are sent with mysqli_query(). Its syntax is mysqli_query("Connection Object", "SQL Query"), so the connection comes first and the SQL string second, as in mysqli_query($con, $sql);. For a SELECT statement it returns a result set holding the matching rows. mysqli_num_rows($result) tells you how many rows came back. To display them, loop through the result with while ($row = mysqli_fetch_array($result)). Each call returns the next row as an array, and the loop ends when no rows are left. Inside the loop you print the fields, often as rows of an HTML table.

Closing the Connection

After the queries are finished, close the connection with mysqli_close(). It takes one argument, the connection object: mysqli_close($con);. Closing releases the memory and the connection slot held on the MySQL server. PHP does close connections when a script ends, but closing them yourself is good practice. A MySQL server allows only a limited number of connections at a time, and on a busy site unclosed connections can use up that limit and make new visitors wait or fail to connect.

Common mistakes to avoid

  • Leaving out the database name in mysqli_connect(). The connection may open, but queries fail because no database is selected. Give all four values in order: server name, user name, password and database name.
  • Running queries without checking whether the connection worked. Test with mysqli_connect_errno() and print mysqli_connect_error() so you know why it failed.
  • Swapping the arguments of mysqli_query(). The connection object comes first and the SQL string second; writing mysqli_query($sql, $con) is wrong.
  • Leaving connections open at the end of the script. Call mysqli_close() with the connection object when the work is done.
  • Calling mysqli_close() before the while loop that fetches the rows. Once the connection is closed the script can no longer run queries on it, so close it only after all the output is printed.

Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.

Frequently asked questions

How do I connect a PHP script to a MySQL database?

Call mysqli_connect() with the server name, user name, password and database name, for example $con = mysqli_connect("localhost", "my_user", "my_password", "Student_DB"). It returns a connection object that you pass to the other mysqli functions.

What arguments does mysqli_connect() take?

As used in the textbook, it takes four values in this order: the server name (usually localhost), the user name, the password and the database name. PHP lets you leave some of them out, but for exam answers always write all four in this order.

How do I check if the database connection failed?

After calling mysqli_connect(), test mysqli_connect_errno() in an if statement. If it returns a non-zero value, the connection failed, and mysqli_connect_error() gives the message, for example echo "Failed to connect to MySQL: " . mysqli_connect_error();

What is the purpose of mysqli_query()?

mysqli_query() sends an SQL statement to the MySQL server and runs it. It takes two arguments, the connection object and the SQL query string. For SELECT it returns a result set; for statements like INSERT or UPDATE it returns true or false.

Why should database connections be closed?

Each open connection uses memory and one of the limited connection slots on the MySQL server. Calling mysqli_close() frees these as soon as your work is done, so other users of the site can connect without delay.

How do I read the records from a query result?

Pass the result to mysqli_fetch_array() inside a while loop: while ($row = mysqli_fetch_array($result)). Each call returns the next row as an array, so you can print its fields, and the loop stops when there are no more rows.

How is mysqli_close() different from mysqli_connect()?

mysqli_connect() opens a connection and needs the server name, user name, password and database name. mysqli_close() ends an existing connection and needs only one argument, the connection object.

Last updated 15 September 2026

More chapters in Computer Applications

View all
1 Multimedia 2 An Introduction to Adobe PageMaker 3 Introduction to Database Management System 4 PHP: Hypertext Preprocessor 5 Functions and Arrays in PHP 6 Conditional Statements in PHP 7 Loops in PHP 8 Forms and Files 10 Introduction to Computer Networks 11 Network Examples and Protocols 12 Domain Name System (DNS) 13 Network Cabling 14 Open Source Concepts 15 E-Commerce 16 Electronic Payment Systems 17 E-Commerce Security Systems 18 Electronic Data Interchange (EDI)