|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2003-10-13 05:24 UTC] temporary1 at understroem dot dk
[2003-10-13 05:26 UTC] temporary1 at understroem dot dk
[2003-10-13 06:30 UTC] mansion@php.net
[2003-10-13 06:40 UTC] lsmith@php.net
[2003-10-13 08:12 UTC] cox@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 19:00:01 2025 UTC |
Description: ------------ It would be nice if empty strings were converted into the database NULL value when you use DB::execute(). Say you have a form in which the user writes a number which is later to be stored in a database column defined as SMALLINT. When the user pushes the submit button, the number will be accessible to the receiving script as, say, $_POST['age']. The script then tries to insert it into the database: $preparation = $db->prepare('INSERT INTO users ( age ) VALUES ( ? )'); $db->execute($preparation, array( $_POST['age'] )); This works fine if the user actually entered his/her age. But say the age form field is optional. Now, the value of $_POST['age'] is an empty string, and when you run the above code, the database will complain that you're trying to insert a string into a SMALLINT column (at least PostgreSQL will behave that way - I don't know about other databases). Most times when a programmer makes code that enters empty string into a database, he/she doesn't actually want the field to contain an empty string - he wants the field to be empty. Thus, it would be nice if PEAR DB converted empty strings into the database NULL value when you run execute(). The alternative is to run a lot of checks for each $_POST variable and then use '!' instead of '?' to insert: $_POST['age'] = (!empty($_POST['age'])) ? $db->quote($_POST['age']) : 'NULL'; $preparation = $db->prepare('INSERT INTO users ( age ) VALUES ( ! )'); $db->execute($preparation, array( $_POST['age'] )); ...but that's a lot of work when you're dealing with a lot of form fields.