0

I have a weird issue which I'm sure is because I don't understand all the idiosyncrasies of MySQL :(

I have a table with a column that has the default value of NULL, "TeamID". When I add a new row without giving a value for that column, it is NULL. Perfect. Except when I wish to update that row, the following code doesn't seem to change the value from NULL (or even cause any error):

    $STH = $this->_db->prepare("UPDATE UserDetails SET 
            TeamID = ':teamID' WHERE UserID = ':userID';");
    $STH->execute($params);

To restate the problem: I have a problem overwriting TeamID with a non-nullable value if it's already NULL. I can't see where there's any error in the code itself, so I'm imagining it's something to do with the NULL value.

One problem coding with PHP/MySQL is that you can't step through your code and look at the contents of the database at the same time -- because PHPMyAdmin gets stepped through, too.

Thanks for any help!

8
  • So TeamID is NULL and not being updated? Commented Feb 29, 2012 at 17:49
  • @BD. Yep, that's exactly correct. Commented Feb 29, 2012 at 17:51
  • And I assume userID is not null? Commented Feb 29, 2012 at 17:53
  • 1
    ':teamID' => :teamID, lose the quotes, around every single placeholder, doesn't matter which type. Commented Feb 29, 2012 at 18:01
  • 1
    PDO does some emulation in some cases, I usually disable that, might have something to do with it (mind you, what if I want to store the string :var in a field? ':var' should insert the literal :var, not go looking for a parameter :var). Casting-wise this would be: ':var' (enter value=>) 'NULL' (cast to int =>) 0 And @Farray was a bit earlier with the answer, although maybe a little less explicit/clear. Commented Feb 29, 2012 at 18:09

2 Answers 2

2

Part of your problem may lie in using quote marks around the parameters. Since you're using a PDO prepared statement with params, you don't have to quote your strings. I'm not sure how PDO represents null, but you might end up in a situation where you try to set SET TeamID = '' or SET TeamID = 'null' and, depending on the column type, that may not be valid.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't put quotes around named parameters with PDO:

$STH = $this->_db->prepare("UPDATE UserDetails SET 
            TeamID = ':teamID' WHERE UserID = ':userID';");
$STH->execute($params);

UserID will never equal the string ':userID', so nothing gets updated.

To ensure that your parameters are interpreted, remove the quotes, like this:

$STH = $this->_db->prepare("UPDATE UserDetails SET 
            TeamID = :teamID WHERE UserID = :userID");
$STH->execute($params);

1 Comment

MySql (in its default config?) will equate 1 with '1', so UserID = '12345' would return the appropriate record. Same thing goes for setting. You can UPDATE...SET col = '1' even if col's type is int. However, you'd get an error with UPDATE...SET col = '' on the same column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.