dg Posted July 18, 2007 Share Posted July 18, 2007 hi all, NULL is converted to blank in php array when i use mysql_fetch_array . I want something where NULL remains as it is in php array thanx --dg Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 like this? <?php if(is_null($row['myvalue'])){ echo 'I am null'; } ?> Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted July 18, 2007 Share Posted July 18, 2007 <?php if(is_empty($row['myvalue'])){ $row['myvalue'] = NULL; } ?> Quote Link to comment Share on other sites More sharing options...
dg Posted July 18, 2007 Author Share Posted July 18, 2007 the soln given convert the database NULL values explicitly.I want something Like while ($aSQL = mysql_fetch_array($sRst)) where $aSQL = array("field_name"=> NULL) and wht i get is $aSQL = array("field_name"=> ) where php converts mysql return NULL values to blanks. Quote Link to comment Share on other sites More sharing options...
lur Posted July 18, 2007 Share Posted July 18, 2007 If you var_dump() the result you'll probably see the data is still NULL, the string value of NULL is indeed blank. If you want to convert the NULL values to readable strings you can do something like this: <?php function convert_null($v) { return ($v === NULL) ? 'NULL' : $v; } while ($data = mysql_fetch_array($query)) { $data = array_map('convert_null', $data); print_r($data); } ?> Quote Link to comment Share on other sites More sharing options...
dg Posted July 18, 2007 Author Share Posted July 18, 2007 $a = array("k"=> NULL,"n"=>'', "c"=>'test'); print_r($a); gives me Array ( [k] => [n] => [c] => test ) but i want it to appear this way Array ( [k] => NULL [n] => [c] => test ) is this possible using any built in php function which can be directly applied to the array? Quote Link to comment Share on other sites More sharing options...
lur Posted July 18, 2007 Share Posted July 18, 2007 If you want to convert NULL to the string "NULL", see my code above. If you just want to see NULL instead of nothing, use var_dump() instead of print_r(). There is no built in function to convert NULL into "NULL" since it doesn't really make sense. Quote Link to comment Share on other sites More sharing options...
dg Posted July 18, 2007 Author Share Posted July 18, 2007 ya .. that true if u want for printing purpose only my prob is something different .... i have two tables in mysql exactly same but in different databases now wht i m doing is fetching the data from table1 and putting it into another table2 something like this : while ($aSQL = mysql_fetch_array($sRst)) { $sInsert = "Insert into table2 (a, b ,c) values ('".$aSQL['a']."', '".$aSQL['b']."', '".$aSQL['c']."')"; mysql_query($sInsert); } now imagine one of the field has a NULL value ..... in table2 it will be insert as blank.. and use of the function like array_map will apply to each element of the array....... and table with 20000 values ?? won't it affect the performance .... i hope it's now clear ..... Quote Link to comment Share on other sites More sharing options...
lur Posted July 18, 2007 Share Posted July 18, 2007 while ($aSQL = mysql_fetch_array($sRst)) { $aSQL['a'] = ($aSQL['a'] === NULL) ? 'NULL' : "'{$aSQL['a']}'"; $aSQL['b'] = ($aSQL['b'] === NULL) ? 'NULL' : "'{$aSQL['b']}'"; $aSQL['c'] = ($aSQL['c'] === NULL) ? 'NULL' : "'{$aSQL['c']}'"; $sInsert = "Insert into table2 (a, b ,c) values ({$aSQL['a']}, {$aSQL['b']}, {$aSQL['c']})"; mysql_query($sInsert); } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 18, 2007 Share Posted July 18, 2007 The reason it is returning as an empty string, is because NULL is only a placeholder, and so you need to instead take the NULL value, and convert it to a string value. Like this: <?php while ($aSQL = mysql_fetch_array($sRst)){ if(is_null($aSQL['myValue'])){ $aSQL['myValue'] = 'NULL'; } echo $aSQL['myValue']; // this will either echo NULL or what was in the database } ?> Quote Link to comment Share on other sites More sharing options...
dg Posted July 19, 2007 Author Share Posted July 19, 2007 hi .... found the solution.... building the insert statement without the fields which has NULL values if ($aSQL['a'] === NULL) { do not include in the insert statement } so by default since field is not present in insert statement it take NULL in the database ... n works fine even if the filed has blank value ...since in the insert, field is included...n goes as blank in the table thanx for the replies Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.