Re: Saving 0 instead of empty string, MySQL

From: Date: Thu, 12 Jul 2007 17:38:37 +0000
Subject: Re: Saving 0 instead of empty string, MySQL
References: 1 2 3 4  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27662@lists.php.net to get a copy of this message
Markus Ernst wrote: > 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? I think those are just set by MySQL create when I don't specify a default -- there shouldn't be one. Though in any case I've found my error and it originates with the PHP substr function and not with the Pear functions. Taking the substring of 0-length string always returns FALSE, even if the start index is 0. The problem here is then FALSE behaves as a Boolean in PEAR, being converted to 0 (logical), but if you attempt to debug, print displays an empty string for FALSE. This threw me off track, sorry. -- edA-qa mort-ora-y Idea Architect http://disemia.com/ Sign: Please digitally sign your emails. Encrypt: I'm also happy to receive encrypted mail.

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.pear.general (#27662) next »