note 85617 modified in function.mysql-affected-rows by danbrown
| From: | danbrown@php.net | Date: | Tue, 28 Apr 2009 01:48:01 +0000 |
| Subject: | note 85617 modified in function.mysql-affected-rows by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153872@lists.php.net to get a copy of this message | ||
Here's a little function I've been using for a while now, pass it two parameters (action
command (1 or 0 see notes)) and a sql statement.
It returns a simple line which shows the length of time taken to action the query, the status of the
query (0= query not actioned, you can set this value for testing, 1=success qry executed
successfully, -1= failed, there was a problem with the sql statement) the number of lines affected
by that query and the sql statement itself.
I've found this invaluable when trying to tie down large amounts of updates to a table, using
this you can easily see where a query was successfully executed and the number of rows are affected,
or where there are problems and a statement has failed for example.
<?php
function dosql($action,$sql){
# assuming you have setup a link to your database entitled $link
# action = 1 run this query
# action = 0 don't run, just return sql statement
$start = getmtime();
if($action==1){
$result = mysql_query($sql);
$affectedrows = "[".mysql_affected_rows($link)."]";
}
return "[".number_format((getmtime()-$start),3)."][$action]: $sql\n";
mysql_free_result($result);
}
?>
Example output:
[0.072][1][80]: UPDATE MYTABLE SET FIELD = 1;
[0.106][1][758]: UPDATE ANOTHERTABLE SET FIELD = 2;
[0.006][-1][0]: UPDATER ANOTHERTABLE SET FIELD = 2;
The output shows:
[Timetaken][result]][lines affected]
The result will be either -1, 0 or 1, -1 means there's a problem with the sql statement, 1
means it executed correctly, 0 means it wasn't executed.
--was--
Here's a little function I've been using for a while now, pass it two parameters (action
command (1 or 0 see notes)) and a sql statement.
It returns a simple line which shows the length of time taken to action the query, the status of the
query (0= query not actioned, you can set this value for testing, 1=success qry executed
successfully, -1= failed, there was a problem with the sql statement) the number of lines affected
by that query and the sql statement itself.
I've found this invaluable when trying to tie down large amounts of updates to a table, using
this you can easily see where a query was successfully executed and the number of rows are affected,
or where there are problems and a statement has failed for example.
function dosql($action,$sql){
# assuming you have setup a link to your database entitled $link
# action = 1 run this query
# action = 0 don't run, just return sql statement
$start = getmtime();
if($action==1){
$result = mysql_query($sql);
$affectedrows = "[".mysql_affected_rows($link)."]";
}
return "[".number_format((getmtime()-$start),3)."][$action]: $sql\n";
mysql_free_result($result);
}
Example output:
[0.072][1][80]: UPDATE MYTABLE SET FIELD = 1;
[0.106][1][758]: UPDATE ANOTHERTABLE SET FIELD = 2;
[0.006][-1][0]: UPDATER ANOTHERTABLE SET FIELD = 2;
The output shows:
[Timetaken][result]][lines affected]
The result will be either -1, 0 or 1, -1 means there's a problem with the sql statement, 1
means it executed correctly, 0 means it wasn't executed.
http://php.net/manual/en/function.mysql-affected-rows.php