|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-11-11 10:35 UTC] w at lder dot de
Description:
------------
The attached Code results a wrong result.
MySQL:
Server Version: 5.0.26
MySQL-Client-Version: 5.0.26
Reproduce code:
---------------
<?php
/*
MySQL:
CREATE TABLE IF NOT EXISTS `test` (
`hereIsDefaultNULL` int(255) default NULL,
`defaultNULLvarchar` varchar(255) default NULL
)
*/
mysql_connect('localhost','user','');
mysql_select_db('test');
$columns_res = mysql_query('SHOW COLUMNS FROM `test`');
while ($fieldRow = mysql_fetch_assoc($columns_res)) {
print_r($fieldRow) ;
}
?>
Expected result:
----------------
Array
(
[Field] => hereIsDefaultNULL
[Type] => int(255)
[Null] => YES
[Key] =>
[Default] => NULL
[Extra] =>
)
Array
(
[Field] => defaultNULLvarchar
[Type] => varchar(255)
[Null] => YES
[Key] =>
[Default] => NULL
[Extra] =>
)
Actual result:
--------------
Array
(
[Field] => hereIsDefaultNULL
[Type] => int(255)
[Null] => YES
[Key] =>
[Default] =>
[Extra] =>
)
Array
(
[Field] => defaultNULLvarchar
[Type] => varchar(255)
[Null] => YES
[Key] =>
[Default] =>
[Extra] =>
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 13:00:01 2025 UTC |
OK, but how can I get the value NULL? I think it is an difference between "NULL" and "". Any idea? Another Example: <?php /* Mysql: CREATE TABLE IF NOT EXISTS `test` ( `hereIsDefaultNULL` int(255) default NULL, `defaultEmptyvarchar` varchar(255) NOT NULL default '' ) */ mysql_connect('localhost','root',''); mysql_select_db('test'); $columns_res = mysql_query('SHOW COLUMNS FROM `test`'); while ($fieldRow = mysql_fetch_assoc($columns_res)) { if ($fieldRow['Default'] == NULL) { echo $fieldRow['Field'] . ' is NULL </br>'; } } ?> Expected result: ---------------- hereIsDefaultNULL is NULL Actual result: -------------- hereIsDefaultNULL is NULL defaultEmptyvarchar is NULLHere the var_dump: array(6) { ["Field"]=> string(17) "hereIsDefaultNULL" ["Type"]=> string(8) "int(255)" ["Null"]=> string(3) "YES" ["Key"]=> string(0) "" ["Default"]=> NULL ["Extra"]=> string(0) "" } array(6) { ["Field"]=> string(19) "defaultEmptyvarchar" ["Type"]=> string(12) "varchar(255)" ["Null"]=> string(2) "NO" ["Key"]=> string(0) "" ["Default"]=> string(0) "" ["Extra"]=> string(0) "" } Yes, I know that "'' == NULL (true)", but in mySQL : mysql> SELECT '' IS NULL; +------------+ | '' IS NULL | +------------+ | 0 | +------------+ mysql> SELECT NULL IS NULL; +--------------+ | NULL IS NULL | +--------------+ | 1 | +--------------+But in PHP ('' == NULL) is true and ('' === NULL) isn't. :)Hi Jani, right, thank you. If someone has a similar problem here my workaround to solve the problem. The goal is to compare an line in a sql Create Table Statement with the actually value in the Database <?php $str = '`hereIsDefaultNULL` int(255) default NULL'; mysql_connect('localhost','root',''); mysql_select_db('test'); $columns_res = mysql_query('SHOW COLUMNS FROM `test`'); while ($fieldRow = mysql_fetch_assoc($columns_res)) { if ($fieldRow['Default'] === NULL) { $fieldRow['Default'] = 'NULL'; } $strToCompare= '`' . $fieldRow['Field'] . '` ' . $fieldRow['Type'] . ' default ' . $fieldRow['Default']; if ($str == $strToCompare) { echo 'OK'; } } ?>