note 36008 added to function.mysql-info
| From: | eric at projectsatellite dot com | Date: | Mon, 22 Sep 2003 18:54:45 +0000 |
| Subject: | note 36008 added to function.mysql-info | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56922@lists.php.net to get a copy of this message | ||
I agree that this is a useful function to use when trying to check on whether an update query
matched a particular row. I created a simple function that returns an associative array with the
values delineated in the returned string.
function get_mysql_info($linkid = null){
$linkid? $strInfo = mysql_info($linkid) : $strInfo = mysql_info();
$return = array();
ereg("Records: ([0-9]*)", $strInfo, $records);
ereg("Duplicates: ([0-9]*)", $strInfo, $dupes);
ereg("Warnings: ([0-9]*)", $strInfo, $warnings);
ereg("Deleted: ([0-9]*)", $strInfo, $deleted);
ereg("Skipped: ([0-9]*)", $strInfo, $skipped);
ereg("Rows matched: ([0-9]*)", $strInfo, $rows_matched);
ereg("Changed: ([0-9]*)", $strInfo, $changed);
$return['records'] = $records[1];
$return['duplicates'] = $dupes[1];
$return['warnings'] = $warnings[1];
$return['deleted'] = $deleted[1];
$return['skipped'] = $skipped[1];
$return['rows_matched'] = $rows_matched[1];
$return['changed'] = $changed[1];
return $return;
}
After trying to update a row that may or may not exist, you can use the above function like so:
$vals = get_mysql_info($linkid);
if($vals['rows_matched'] == 0){
mysql_query("INSERT INTO table values('val1','val2',
'valetc')", $linkid);
}
----
Manual Page -- http://www.php.net/manual/en/function.mysql-info.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36008
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36008&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36008&report=yes