Advertisement
bpoole

Count rows affected by update/delete MySQL query

Jan 9th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2. //take an update or delete MySQL query and generate a query that gets a count of the rows that will be affected by the query, prints that number, then runs the original update/delete query
  3. function run_query($query){
  4.     $query = "update Users set Name = 'foobar' where ID = 1";
  5.     //$query = "delete from Users where ID = 1;
  6.     $cnt_query = preg_replace('/^(update|delete)\s(from\s)?(\w+)[\w\s\=\'\,]+(where.*)$/i', 'select count(*) as cnt from $3 $4', $query);
  7.     $cnt_line = mysql_fetch_assoc(mysql_query($cnt_query));
  8.     echo $cnt_line['cnt'] . "\n";
  9.     mysql_query($query);
  10. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement