note 85617 added to function.mysql-affected-rows
| From: | sean at adtools dot co dot uk | Date: | Tue, 09 Sep 2008 08:48:32 +0000 |
| Subject: | note 85617 added to function.mysql-affected-rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144259@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.
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.
----
Server IP: 87.124.35.190
Probable Submitter: 195.152.249.4
----
Manual Page -- http://www.php.net/manual/en/function.mysql-affected-rows.php
Edit -- https://master.php.net/note/edit/85617
Del: integrated -- https://master.php.net/note/delete/85617/integrated
Del: useless -- https://master.php.net/note/delete/85617/useless
Del: bad code -- https://master.php.net/note/delete/85617/bad+code
Del: spam -- https://master.php.net/note/delete/85617/spam
Del: non-english -- https://master.php.net/note/delete/85617/non-english
Del: in docs -- https://master.php.net/note/delete/85617/in+docs
Del: other reasons-- https://master.php.net/note/delete/85617
Reject -- https://master.php.net/note/reject/85617
Search -- https://master.php.net/manage/user-notes.php