Oliver Spryn
Oliver Spryn

Reputation: 17358

mysql_affected_rows Returns False On Successful Query?

I am having a difficult time sorting through this PHP/MySQL issue. Let me show you my database, and explain my situation:


Create table:

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `active` varchar(20) NOT NULL,
  `activation` varchar(15) NOT NULL,
  `firstName` longtext NOT NULL,
  `lastName` longtext NOT NULL,
  `passWord` longtext NOT NULL,
  `changePassword` text NOT NULL,
  `emailAddress1` longtext NOT NULL,
  `emailAddress2` longtext NOT NULL,
  `emailAddress3` longtext NOT NULL,
  `role` longtext NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `name` (`firstName`,`lastName`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

Insert a value:

INSERT INTO `users` (
  `id` ,
  `active` ,
  `activation` ,
  `firstName` ,
  `lastName` ,
  `passWord` ,
  `changePassword` ,
  `emailAddress1` ,
  `emailAddress2` ,
  `emailAddress3` ,
  `role`
) VALUES (
  NULL,  '1000000000',  'abcdefghijklmno',  'John',  'Smith',  '*24D7FB97963C40FE5C56A6672F9560FC8B681508',  'on',  '[email protected]',  '',  '',  'User'
);

Update a value:

$affected = mysql_query(UPDATE users SET passWord = PASSWORD('a9eb42e1b3be829ef42972ea9abab334'), changePassword = 'on' WHERE emailAddress1 = '[email protected]', $dbID); 

if (mysql_affected_rows($affected)) {
    //Never runs
}

The above UPDATE query executes just fine in my script, phpMyAdmin, and the MySQL terminal. However, mysql_affected_rows($affected) always gives me this error:

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given

I know that this means my query failed, but every time I go into the database, I see that the values have been updated.

Removing the parameter from the function appears to clear things up. However, I rather have the identifier as the function parameter, just to be sure what I am referring to, and for code insurance.

Any idea why this might be doing this?

Thank you for your time.

Upvotes: 0

Views: 3227

Answers (3)

Amir Mehrnam
Amir Mehrnam

Reputation: 550

In my case, it was that the new value of updating was equal to the old value, so no change is made and the result of the mysql_affected_rows() would be 0 or false

Upvotes: 0

mohammad falahat
mohammad falahat

Reputation: 748

change:

$affected = mysql_query(UPDATE users SET passWord = PASSWORD('a9eb42e1b3be829ef42972ea9abab334'), changePassword = 'on' WHERE emailAddress1 LIKE '[email protected]', $dbID);

and execute

Upvotes: 1

deceze
deceze

Reputation: 522332

http://php.net/mysql_query

Return Values

...

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

And:

int mysql_affected_rows ([ resource $link_identifier = NULL ] )

This means mysql_affected_rows wants a mysql connection resource as an argument. Not the result of mysql_query, and most certainly not if that result is only true or false. You use it like this:

$successful = mysql_query('UPDATE ...'); 

if ($successful) {
    echo 'Affected rows: ' . mysql_affected_rows();
} else {
    echo 'Fail: ' . mysql_error();
}

Upvotes: 5

Related Questions