ROW COUNT - (MySQL Misc)
In MySQL, the ROW COUNT function is used to return the number of rows that were affected by the last statement executed. In this tutorial, we'll discuss the syntax, example, output, explanation, use, important points, and summary of the ROW COUNT function in MySQL.
Syntax
The syntax for the ROW COUNT function in MySQL is as follows:
mysql_affected_rows()
This function returns an integer value that represents the number of rows that were affected by the last statement executed.
Example
Let's say we have a table called "customers" and we want to update one of the rows. Here's how we can use the ROW COUNT function to get the number of rows that were affected:
UPDATE customers SET city='New York' WHERE customer_id=5;
SELECT ROW_COUNT();
In this example, we first update the "city" column for the row where the "customer_id" is 5. We then use the ROW COUNT function to get the number of rows that were affected.
Output
When we run the example code above, the output will be:
1
This is because we updated one row with the "customer_id" of 5.
Explanation
The ROW COUNT function in MySQL returns the number of rows that were affected by the last statement executed. In our example, the "UPDATE" statement affected only one row, so the ROW COUNT function returns 1.
Use
The ROW COUNT function in MySQL is useful for getting the number of rows that were affected by the last statement executed. This can be helpful for debugging or for verifying that a query or statement executed successfully.
Important Points
- The ROW COUNT function returns the number of rows that were affected by the last statement executed.
- The function can be used with all kinds of statements, including "INSERT", "UPDATE", "DELETE", and "REPLACE".
- The ROW COUNT function can only be called immediately after the statement has been executed.
Summary
In this tutorial, we discussed the ROW COUNT function in MySQL. We covered the syntax, example, output, explanation, use, and important points of this function. With this knowledge, you can now use the ROW COUNT function in your MySQL queries to get the number of rows that were affected by the last statement executed.