How does mysqli_affected_rows() work for SELECT statements in PHP?
mysqli_affected_rows() function is used to get the number of rows affected by INSERT, UPDATE, or DELETE queries in MySQL. However, for SELECT statements, this function will always return 0 because SELECT queries do not affect rows in the database. To get the number of rows returned by a SELECT query, you can use mysqli_num_rows() function instead.
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Execute SELECT query
$result = mysqli_query($conn, "SELECT * FROM table");
// Check if query was successful
if($result) {
// Get the number of rows returned
$num_rows = mysqli_num_rows($result);
// Display the number of rows
echo "Number of rows returned: " . $num_rows;
} else {
echo "Error executing query: " . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
Related Questions
- What security measures should be taken when incorporating user input (such as $_GET variables) in PHP code within HTML templates?
- How can you reverse the order of the array using array_reverse in PHP?
- In what scenarios would it be more appropriate to use a different programming language, such as C, instead of PHP for certain tasks?