php - MYSQL gives same response '0 rows affected' in different situations -
mysql update query give same response in different situations
"0 rows affected. (query took 0.0789 sec)"
1) clause not matched
2) clause matched given value same existing value
ex:
assume user_id = '86' not exist in table
update `undergraduate` set `faculty` = 'efac' `user_id` = '86' assume user_id = '86' exist. value 'efac' in there
update `undergraduate` set `faculty` = 'efac' `user_id` = '86' question better way identify both conditions ?
i'm using : mysql :
+-------------------------+ | @@version | +-------------------------+ | 5.6.19-0ubuntu0.14.04.1 | +-------------------------+ php : 5.5.9-1 (pdo)
mysql command line
if using mysql command line tool output given let differentiate 2 scenarios.
if entry doesn't exist:
+---------------+---------+ | user_id | faculty | +---------------+---------+ | 80 | abc | +---------------+---------+ mysql> update `undergraduate` set `faculty` = 'efac' `user_id` = '86'; query ok, 0 rows affected (0.00 sec) rows matched: 0 changed: 0 warnings: 0 if entry exist, isn't updated:
+---------------+---------+ | user_id | faculty | +---------------+---------+ | 80 | abc | | 86 | efac | +---------------+---------+ mysql> update `undergraduate` set `faculty` = 'efac' `user_id` = '86'; query ok, 0 rows affected (0.00 sec) rows matched: 1 changed: 0 warnings: 0 if entry exist, , updated:
+---------------+---------+ | user_id | faculty | +---------------+---------+ | 80 | abc | | 86 | bcd | +---------------+---------+ mysql> update `undergraduate` set `faculty` = 'efac' `user_id` = '86'; query ok, 0 rows affected (0.00 sec) rows matched: 1 changed: 1 warnings: 0 php - pdo
pdo unfortunately doesn't provide mechanism both rows matched and rows changed query, can choose 1 want. default return number of rows changed, option can specified on connection return number of rows match instead:
$db = new pdo('mysql:dbname=database;host=host', 'username', 'password', array( pdo::mysql_attr_found_rows => true )); php - mysqli
the mysqli functions can same pdo in terms of choosing between matched , updated:
$db = mysqli_init(); $db->real_connect('host', 'username', 'password', 'database', '3306', null, mysqli_client_found_rows); but can call function mysqli_info() / $db->info() (see http://php.net/manual/en/mysqli.info.php) , return string matched / updated rows parse:
records: 3 duplicates: 0 warnings: 0
Comments
Post a Comment