Results 1 to 3 of 3

Thread: MySQL ignores UPDATE when data is the same?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    MySQL ignores UPDATE when data is the same?

    Hello all.
    I just discovered (correct me if I'm wrong), that MySQL ignores an UPDATE when the data is exactly the same as already in the database.

    For instance, I have this query:
    Code:
    UPDATE cms_users SET user_email="[email protected]", internal_notes="blah blah blah" WHERE cms_users.user_id=8
    I pass that to a class function which executes the query and returns the number of affected rows:
    Code:
    $affected_rows=$database->execute($query);
    Then I was testing it:
    Code:
    /* DETERMINE IF THE QUERY WAS SUCCESSFUL */
    	if(
    		$affected_rows>0
    	) {
    
    		/* SUCCESS */
    			/* REDIRECT BY CALLING THE FUNCTION WHICH BUILDS A REDIRECT URL */
    				header('Location: '.admin_redirect_url('thank'));
    
    	} else {
    
    		/* FAILURE */
    			/* REDIRECT BY CALLING THE FUNCTION WHICH BUILDS A REDIRECT URL */
    				header('Location: '.admin_redirect_url('err05'));
    
    	}
    Once I actually changed the data, the value returned by the class function went from 0 to 1.

    So my questions are:
    1) is it MySQL or PHP which is doing this?
    2) is there a way of "forcing" the database to update, without say, adding a timestamp?

    Many thanks.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: MySQL ignores UPDATE when data is the same?

    I'm not sure of an easy way to check, but I would think that MySQL itself would not bother updating a record that had the same value. MySQL would do it all internally, whereas PHP is just being passed an SQL query and is sending it to MySQL. For PHP to do this check, it seems like it would be -very- inefficient. I doubt there's any way of forcing it to update, either. There's no point in MySQL updating a record if there is nothing to update ;)

    From the looks of it, you're just checking to see if your query was successful. Don't use mysql_affected_rows(), and instead just do:
    PHP Code:
    if(mysql_query("UPDATE table SET field='value' WHERE field2='value2' LIMIT 1")){
      
    //success
    }else{
      
    //failure

    It should produce the result you're looking for.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: MySQL ignores UPDATE when data is the same?

    Thanks kows.

    Just found this at the MySQL site:
    "If you set a column to the value it currently has, MySQL notices this and does not update it."

    I could write another class function, as you describe, which returns a boolean upon success/failure. I guess I was just being lazy and trying to use the same function for multiple purposes. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width