Re: Saving 0 instead of empty string, MySQL

From: Date: Thu, 12 Jul 2007 16:58:59 +0000
Subject: Re: Saving 0 instead of empty string, MySQL
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27661@lists.php.net to get a copy of this message
edA-qa mort-ora-y schrieb:
I have a problem where the string 0 is being saved to my database rather than the empty string which I am requesting. The code looks like: $es = $db->prepare( "UPDATE fs_fortress SET Player=?, End=?, Points=? WHERE ID=?" ); $res = $db->execute( $es, array( $player, mysql_datestr( time() ), $points, $id ) ); Where $player is always of string type and works as long as it is non-empty. As soon as $player is an empty string, 0 gets stored.
what are the results of issuing "describe fs_fortress;" ? I'm guessing the default value is set to 0 ;->
mysql> desc fs_fortress; +------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| ID         | int(11)     | NO   | PRI | NULL    | auto_increment |
| Player     | varchar(25) | NO   | MUL | NULL    |                |
| Start      | datetime    | NO   |     | NULL    |                |
| End        | datetime    | YES  |     | NULL    |                |
| Points     | int(10)     | NO   |     | 0       |                |
| Difficulty | int(2)      | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+ 6 rows in set (0.06 sec) Though, even if the default were some string, since I am explicitly passing a parameter for this column the default should not be used.
You have several fields with NOT NULL, but a default of NULL. I don't know if this is the source of your problem, but I guess that this kind of inconsistencies might lead to unpredictable results for some actions on the table. Does the problem persist if you fix this, setting the default for Player to an empty string? BTW, try to bottom-post (add your comments _after_ the parts you quote), which makes the discussion more readable. This behaviour is actually required in this kind of mailing lists and newsgroups, which have other rules than normal e-mail. HTH Markus

« previous php.pear.general (#27661) next »