I have following query in ColdFusion:
<cfquery name="test" datasource="test">
INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery>
I want to find number of affected rows count. Is there any way to find this in ColdFusion?
mysql_affected_rows() may be called immediately after executing a statement with mysql_real_query() or mysql_query() . It returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE , DELETE , or INSERT . For SELECT statements, mysql_affected_rows() works like mysql_num_rows() .
MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.
<cftransaction>
<cfquery name="test" datasource="test">
INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery>
<cfquery name="test1" datasource="test">
SELECT ROW_COUNT() AS numberOfRowsAffected
</cfquery>
</cftransaction>
<cfdump var="#test1.numberOfRowsAffected#">
Or
You can make changes in the settings to allow multiple sql statements withing one cfquery. To make that happen, make changes as mentioned below:
In ColdFusion Admin, go to your Data Source definition form and add allowMultiQueries=true to the Connection String box. Once you do that, you can pass multiple semi-colon separated queries in a single CFQUERY tag. Just make sure you're using CFQUERYPARAM to screen for SQL Injection Attacks.
Try this one.
<cfquery datasource="test" result="myResult">
INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery>
<cfset getNumberOfRecords = listLen(#myResult.generated_key#)>
<cfdump var="#getNumberOfRecords#">
myResult.generated_key
contains the list of generated ids, so we can find how many rows have been inserted when we use listLen()
function.
Use the result
attribute and then access the recordCount
key of the result.
<cfquery result="myResult" datasource="test">
INSERT INTO test (id,name) VALUES (1,"santy"),(2,"raj"),(3,"nil")
</cfquery>
<cfdump var="#myResult.recordCount#">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With