As a solution to the problem pointed in the post reffering to mysql_affected_rows() returning 0 when you are making an update query and the fields are not modified although the query is valid, i'm posting the following function. It is very simple and based on a previous post.
function mysql_modified_rows () {
$info_str = mysql_info();
$a_rows = mysql_affected_rows();
ereg("Rows matched: ([0-9]*)", $info_str, $r_matched);
return ($a_rows < 1)?($r_matched[1]?$r_matched[1]:0):$a_rows;
}
Hope you'll find it usefull.
mysql_info
(PHP 4 >= 4.3.0, PHP 5, PECL mysql:1.0)
mysql_info — 가장 최근 질의에 대한 정보를 반환
설명
최근 질의에 대한 상세한 정보를 반환한다.
매개변수
- link_identifier
-
MySQL 연결. 지정하지 않으면 mysql_connect()로 연 마지막 연결을 사용합니다. 연결이 없으면, 매개변수 없이 mysql_connect()를 호출하여 연결을 만듭니다. 연결이 성립되지 않으면 E_WARNING 경고를 생성합니다.
반환값
성공하면 진술문(statement) 정보를, 실패하면 FALSE를 반환한다. 진술문이 제공하는 정보와 반환되는 값이 어떻게 보여지는지 아래의 예제를 참고하라. 진술문은 FALSE를 반환하는 Returns information about the statement on success, or FALSE on failure. See the example below for which statements provide information, and what the returned value may look like. Statements that aren't listed will return FALSE
예제
Example#1 관련된 MySQL 진술문
진술문은 문자열로 반환된다. 숫자들은 질의와 관련된 값이며 설명을 목적으로한 용도이다.
INSERT INTO ... SELECT ... String format: Records: 23 Duplicates: 0 Warnings: 0 INSERT INTO ... VALUES (...),(...),(...)... String format: Records: 37 Duplicates: 0 Warnings: 0 LOAD DATA INFILE ... String format: Records: 42 Deleted: 0 Skipped: 0 Warnings: 0 ALTER TABLE String format: Records: 60 Duplicates: 0 Warnings: 0 UPDATE String format: Rows matched: 65 Changed: 65 Warnings: 0
주의
Note: 다중 값 목록이 기술된 INSERT ... VALUES 구문에서 mysql_info()는 FALSE가 아닌 값을 반환한다.
mysql_info
24-Apr-2006 12:18
09-Nov-2005 03:25
Imade a quick conversion of eric's function just to count matched or affected rows from a query.
/**GD gdf_db_count_query_v1: returns the amount of rows matched or affected by the last query. Must be used immediately after the concerned query.
*/
function gdf_db_count_query($link = 'dbh') {
$info_str = mysql_info($$link);
if (ereg("Records: ([0-9]*)", $info_str, $count) == false) {
ereg("Rows matched: ([0-9]*)", $info_str, $count);
}
return $count;
}
22-Sep-2003 02:54
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);
}
06-Sep-2002 04:44
This function can be used as a workaround for a misfeature of MySQL: on an UPDATE, rows that aren't updated _solely because they looked the same before_ will not be seen in mysql_affected_rows(). This causes problems when you want to use the result of the update to determine if there's need to do an INSERT. With MySQL you can do an INSERT IGNORE if there's no risk of if failing because of a duplicate key other than the one used in the UPDATE. However, if this isn't the case or you want a bit of RDBMS independence, there's no easy/pretty workaround. I think I'll resort to doing a SELECT to determine the primary key before doing the update/insert, as using the CVS version of PHP isn't an option for me.
