PHP and MySQL Databases
Chapter 9 · Tamil Nadu State Board
- 3 min read
- 6-question quiz
Every key point below shows the sentence from your textbook it comes from. How we check content
In short
This lesson explains how PHP connects to a MySQL database and uses SQL to retrieve or change data. The MySQLi functions introduced include connecting to a database, running queries and closing a connection. Understanding these steps helps students use database information in PHP web applications. The lesson also introduces web databases and describes some features and uses of PHP.
Written by AI from this lesson. How we check it
Key points
- PHP and MySQL can work together to perform database operations such as inserting, retrieving, updating and deleting data.
From your textbook
“perform major operations like INSERT, SELECT, UPDATE and DELETE using Structured query language (SQL).”
- The MySQLi extension provides PHP functions for connecting to and managing MySQL databases.
From your textbook
“MySQLi is extension in PHP scripting language which gives access to the MYSQL database.”
- The connection function needs the server name, username, password and database name.
From your textbook
“Database Server name, Database username, password and Database Name.”
- If a connection fails, the code can show an error; if it succeeds, it can report a successful connection.
From your textbook
“If connection gets fail, output will be printed with MySQL error code. Otherwise connection is success.”
- PHP uses mysqli_query() to execute an SQL statement through a database connection.
From your textbook
““mysqli_query” is a function, that helps to execute the SQL query statements in PHP scripting language.”
- The mysqli_close() function ends an already opened connection between PHP and the MySQL server.
From your textbook
“mysqli_close() Function is used to close an existing opened database connection between PHP scripting and MySQL Database Server.”
- PHP is a server-side scripting language designed for web development and can connect to MySQL using built-in functions.
From your textbook
“PHP has built-in function which can be easily connected to MySQL database”
Formulas in this chapter
- Establish a database connectionmysqli_connect("Server Name", "User Name", "Password", "DB Name");Explained here
- Run an SQL querymysqli_query("Connection Object", "SQL Query")Explained here
- Close a database connectionmysqli_close("Connection Object");Explained here
Why PHP connects to MySQL
A database stores and organises data. PHP can connect to a MySQL database so a web application can retrieve or change that data. SQL statements such as INSERT, SELECT, UPDATE and DELETE are used for these operations.
Definition
- MySQLi
- MySQLi is a PHP extension that provides access to a MySQL database.
Main steps in PHP and MySQL connectivity
- Connect PHP to the database server.
- Select the database as part of the connection.
- Run an SQL statement.
- Close the connection when finished.
MySQLi connection, query and close functions
mysqli_connect("Server Name", "User Name", "Password", "DB Name");
The four parameters are the database server name, username, password and database name.
Store the connection details in variables and pass them to mysqli_connect(). If the connection fails, the program can display a MySQL error. If it succeeds, the program can continue.
mysqli_query("Connection Object", "SQL Query")
The first parameter is the connection object. The second parameter is the SQL statement to execute.
Worked example
Run a query to retrieve student names and ages.
Try it first, then show the solutionSolution
- Connect to the Student_DB database: $con=mysqli_connect("localhost","my_user","my_password","Student_DB ");
- Set the query: $sql="SELECT student_name,student_age FROM student";
- Run it using mysqli_query($con,$sql);
Answer: The query requests the student_name and student_age fields from the student table.
mysqli_close("Connection Object");
The parameter is the connection object to close.
Three important MySQLi functions
| Function | Purpose |
|---|---|
| mysqli_connect() | Connects PHP to a MySQL database. |
| mysqli_query() | Executes an SQL statement. |
| mysqli_close() | Closes an opened database connection. |
Retrieving database records with PHP
A query can retrieve records from a database. PHP can count the returned rows and fetch records one at a time. It can then display fields such as names, contact numbers, email addresses, cities and countries in the browser.
What the sample program does
- Connects to a database using mysqli_connect().
- Checks whether the connection failed and displays an error if needed.
- Runs a SELECT query on my_contacts.
- Counts returned rows with mysqli_num_rows($result).
- Fetches records with mysqli_fetch_array($result) and displays their fields.
- Closes the connection using mysqli_close($connection).
Remember: Remember the order: connect, run the query, use the returned data, then close the connection.
PHP and database terms
Definition
- SQL
- SQL is a language used to manage data held in a relational database management system.
Definition
- Query
- A query is a request for data or information from one or more database tables.
Definition
- MySQL
- MySQL is an open-source relational database management system.
Definition
- Server
- A server is a computer or device that provides functionality to other programs or devices called clients.
Definition
- Database
- A database is an organised collection of data that is generally stored and accessed electronically.
PHP features
- PHP is open source and has community support.
- It is a server-side scripting language designed for web development.
- It can be embedded with HTML and client-side scripting language.
- It has built-in functions for connecting to MySQL.
- Many webservers can support PHP.
- Many software frameworks support PHP.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is MySQLi in PHP?
MySQLi is an extension in PHP that gives access to a MySQL database. Its important functions include mysqli_connect() for connecting, mysqli_query() for executing SQL statements and mysqli_close() for closing an opened connection.
How do I connect PHP to a MySQL database?
Use mysqli_connect() with four parameters: the database server name, username, password and database name. For example, mysqli_connect("localhost", "username", "password", "School_DB"); attempts to connect PHP to that database.
What does mysqli_query() do?
mysqli_query() executes an SQL statement through a database connection. It takes two parameters: the connection object and the SQL query. For example, mysqli_query($con, $sql); runs the statement stored in $sql using the connection stored in $con.
How do I close a MySQL connection in PHP?
Use mysqli_close() and give it the connection object. For example, mysqli_close($con); closes the connection stored in $con. This function is used to close an existing connection between PHP and the MySQL database server.
What are the main steps in PHP and MySQL connectivity?
First, connect PHP to the database server using mysqli_connect(). Next, execute an SQL statement with mysqli_query(). Use the returned data as needed, then close the connection with mysqli_close(). A query can retrieve or manipulate data in the database.
Practice
Which function is used to execute SQL statements in PHP?
Exam questions
Practice questions with model answers. These are not from an official paper.
Name three MySQLi functions used for database connectivity and management.2 marks
The functions are mysqli_connect(), mysqli_close() and mysqli_query().
What information is needed by mysqli_connect() to connect to a database?2 marks
It needs the database server name, database username, password and database name.
Explain the purposes of mysqli_query() and mysqli_close().2 marks
mysqli_query() executes an SQL statement in PHP using a connection. mysqli_close() closes an existing connection between PHP and the MySQL database server.
Describe how a PHP program can connect to MySQL, run a query and close the connection.5 marks
PHP connects to a MySQL database using the MySQLi functions. The process involves establishing a connection, executing an SQL query and closing the connection.
- Establish the connection: Use mysqli_connect() to connect PHP to the database server. It requires the server name, username, password and database name.
- Store the connection: Assign the result of mysqli_connect() to a connection variable, such as $conn or $con, so it can be used in later operations.
- Check the connection: Check whether the connection was successful. If it fails, the program can display the connection error.
- Prepare the SQL query: Write the required SQL statement and store it in a variable. For example, a SELECT statement can retrieve fields from a table.
- Run the query: Call mysqli_query() with the connection object and SQL query. This executes the query through PHP.
- Close the connection: After the database work is complete, call mysqli_close() with the connection object to close the opened connection.
Example: $con=mysqli_connect(“localhost”,”my_user”,”my_password”,”Student_DB “); $sql=”SELECT student_name,student_age FROM student”;mysqli_query($con,$sql);
Thus, a PHP program connects to MySQL with mysqli_connect(), runs SQL using mysqli_query() and ends the connection with mysqli_close().
Watch out
- Do not confuse connecting to the database with executing a query: mysqli_connect() establishes the connection, while mysqli_query() runs the SQL statement.
- Do not confuse closing a connection with deleting database data; mysqli_close() ends the connection.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →