In PHP, you can execute a select query using the mysqli_query() function. This function sends a query to the database and returns a result set object, which you can then use to fetch data from the database.
Here’s an example of how to execute a select query in PHP:
// Connect to the database
$conn = mysqli_connect(“localhost”, “username”, “password”, “database”);
// Execute the select query
$result = mysqli_query($conn, “SELECT * FROM users”);
// Check if the query was successful
if ($result) {
// Loop through each row in the result set
while ($row = mysqli_fetch_assoc($result)) {
// Do something with the data
echo $row[“id”] . ” ” . $row[“name”] . “<br>”;
}
} else {
// Handle the error
echo “Error: ” . mysqli_error($conn);
}
// Free up memory used by the result set
mysqli_free_result($result);
// Close the database connection
mysqli_close($conn);
In this example, we first connect to the database using mysqli_connect(). We then execute a select query using mysqli_query(), which retrieves all rows from the “users” table. We check if the query was successful using an if statement, and if so, we loop through each row in the result set using mysqli_fetch_assoc() and do something with the data. We also free up the memory used by the result set using mysqli_free_result() and close the database connection using mysqli_close().
Note that when executing a select query, it’s important to properly handle any errors that may occur. In this example, we use mysqli_error() to display the error message if the query was not successful. Additionally, it’s important to free up the memory used by the result set using mysqli_free_result() to prevent memory leaks.