In the field of web development using PHP and MySQL, there are several functions available to execute SQL queries. One commonly used function is the "mysqli_query()" function.
The "mysqli_query()" function is specifically designed for executing SQL queries in PHP. It takes two parameters: the first parameter is the database connection object, and the second parameter is the SQL query that you want to execute. The function returns a result object, which can be used to fetch the data returned by the query.
To use the "mysqli_query()" function, you first need to establish a connection to the MySQL database using the "mysqli_connect()" function. This function takes several parameters, including the host name, username, password, and database name. Once the connection is established, you can pass the connection object as the first parameter to the "mysqli_query()" function.
Here is an example of how to use the "mysqli_query()" function to execute a simple SQL query in PHP:
php
<?php
// Establishing a connection to the MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Checking if the connection was successful
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Executing a SQL query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
// Checking if the query was executed successfully
if ($result) {
// Fetching the data from the result object
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . ", Email: " . $row["email"] . "<br>";
}
} else {
echo "Error executing query: " . mysqli_error($connection);
}
// Closing the database connection
mysqli_close($connection);
?>
In the above example, we first establish a connection to the MySQL database using the "mysqli_connect()" function. We then execute a simple SQL query to select all rows from the "users" table. The result of the query is stored in the "$result" variable. We then use the "mysqli_fetch_assoc()" function to fetch each row of data from the result object and display it on the web page.
It is important to note that the "mysqli_query()" function can be used to execute any type of SQL query, including SELECT, INSERT, UPDATE, and DELETE statements. However, it is important to sanitize user input and use prepared statements to prevent SQL injection attacks.
The "mysqli_query()" function is a fundamental tool in PHP for executing SQL queries. It enables developers to interact with MySQL databases and retrieve data based on their specific requirements. By utilizing this function, developers can create dynamic and interactive web applications that leverage the power of databases.
Other recent questions and answers regarding Examination review:
- What steps should be taken to ensure the security of user-entered data before making queries in PHP and MySQL?
- How do we fetch the result of the query as an associative array in PHP?
- How can we construct the SQL query to retrieve a specific record from a table based on a given ID?
- What are the steps involved in retrieving a single record from a MySQL database using PHP?

