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 are the advantages of using arrays to store variables for different layouts in PHP?
- How can PHP be used to check for correct translations in a language learning application?
- In what scenarios is it more appropriate to use JavaScript instead of PHP for client-side functionality in web development?