I have a simple MySQLi statement with bound results. One of the fields in that recordset is a double and results can be either NULL, 0 or other numeric value.
$wheels_cost is the recordset bound result.
The following logic should ignore null values and only process zero or other numeric values.
if($wheels_cost <> NULL){
carry out a process
} else {
the value is null so ignore
}
For some reason, the results I'm getting seem to reflect that the result is a zero rather than a null - which is not the desired result.
I've checked the DB and the field is null.
I need to tell the difference between a null and a zero value.
I've only tried the above logic and can't think why it doesn't work. Should I be using isset() or is_null() or empty()?
Thanks for feedback
Finished code below:
if(!is_null($wheels_cost)){
carry out a process
} else {
the value is null so ignore
}
Worked perfectly
Try this:
if($wheels_cost is not NULL){
# carry out a process
} else {
# the value is null so ignore
}
I do not use mySQLi but I think the syntax should look like PostGreSQL
In PostGreSQL, the NULL value can not be equal or differentiated to any column or variable, since it is an unknown value and of an unknown type.
Examples here: https://www.techonthenet.com/mysql/is_null.php
Related
I have a script that geocodes physical addresses and puts it into a MySQL database with two columns: coord_lat and coord_long. These are float type columns but unfortunately some addresses don't get geocoded correctly and the script tries to push the address as a null value into the database which then breaks the script because the database cannot hold a null value.
I'm trying to find a way I can rewrite my script to determine if the geocoded address comes out to a null value to automatically rewrite that value to 0 so that the database and script don't break.
Can anyone give me some advice on switching/replacing a null values to 0?
try this:
if PHP (shorthand if)
(is_null($value) ? 0:$value)
else if MySQL (coalesce function)
SELECT COALESCE(#your_value_here,0) FROM dual
for reference of coalesce:
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
for shorthand if statement (ternary):http://davidwalsh.name/php-ternary-examples
If the value is NULL, then assign 0
if(is_null($value)){
$value = 0;
}
I have a table with the column data type like this:
price decimal(6,2) unsigned NOT NULL,
And my validation code is like this:
if ( (!empty($_POST['price'])) && (is_numeric($_POST['price'])) && (isset($_POST['price'])) > 0 ) {
$price = ($_POST['price']);
} else{
$price = FALSE;
echo '<p> Only accept number and must be higher than zero</p>';
}
I use the $_POST form for users to submit the value. But you know,
1/ When the user types any non-numeric value such as a,b,c etc, it also validates well.
2/ When the user types the value zero in, it validates well too.
However, the question is that when I tested it with no value typed in. I mean that I left the value empty and hit the 'submit' button, the error message still returned as per the }else { part does, but the value was still inserted into the table column with a value of 0.00 technically at the same time.
To my limited knowledge, I can guess that the problem was probably at the Mysqli data type of the table I chose, but i don't know how to correct it.
Can you help me, please?
I think the solution you're looking for is to simply move the inserting code to the first if statement. That way it'll only insert the value if it is numeric and not empty.
What you describe means that you've failed to stop the insert when $price===false
(i.e. the problem is not the evaluation; it has given you the correct message. You've some programming logic error elsewhere)
I wrote some PHP code to retrieve some data from my MySQL database.
My NetBeans debugger (v7.4) shows all array elements and related keys (of $data_array_from_db) which keys corresponds with the column fields in a the database table, EXCEPT the keys of which database fields are not filled (value NULL).
However, when the array_key_exists() function is executed in the code below for say 'akey' that corresponds with such non filled database field, array_key_exists() returns a 'true' value (instead of expected false) - as if the key does exist (while again, the NetBeans debugger does not show the $data_array_from_db['akey']).
I know for sure that the database function array_key_exists() works correctly. Am I interpreting something wrong? Does/should the key exist if its corresponding database value is NULL?
$data_array_from_db = $corpdb->GetSpecificDBRecords($sqlquery5); //GetSpecificDBRecords() includes some PDO statements
if (array_key_exists('akey', $data_array_from_db)) { // Database value is optional; exists?
$response_array[0]['akey'] = $data_array_from_db['akey']; // This line executed while akey does not show in NetBeans debuggers
}
else { // Database value does not exist; set to 0
$response_array[0]['akey'] = 0;
}
NULL is a value, and you need to check for it if you want to replace it. Change:
if(array_key_exists('akey',$data_array_from_db) ){
To:
if(array_key_exists('akey',$data_array_from_db) && !is_null($data_array_from_db['akey'])){
Or just check if it exists and it's not empty:
if (!empty($data_array_from_db['akey'])) {
I have searched the site and although I have found questions and answers similar I haven't been able to find an answer. After 4 hours of searching I've decided to bite the bullet and ask the question.
I have 4 date fields in a form that aren't required. I would like it to enter a date into the database if one of the fields has an entry or null if any are left blank.
I have an if statement that checks if the value is empty and if so $value = null, otherwise use $value = date("Y-m-d",strtotime($_post['value'])) to convert it to a date and this works well.
The problem is in my query. If I use '$value' it will insert the date correctly but won't insert a null value because using 'null' makes sql think it's a string. If I use just $value the null inserts just fine but the date goes in as 0000-00-00.
Any advice would be very much appreciated
Thanks for the advice so far...
Null is allowed, this is my script...
if(empty($_POST['fp32_original_install_date'])){
$fp32_install = NULL;
}else{
$fp32_install = date("Y-m-d",strtotime($_POST['fp32_original_install_date']));
}
$sql = "INSERT INTO accounts_cstm (id_c, support_c, install_date_c, sware_renewal_date_c, product_key_c, account_status_c, fp32_support_type_c, fp32_support_renewal_date_c, fp32_original_install_date_c) VALUES ('$Guid','$cdr_support', '$cdr_install', '$cdr_renew', '$prod_key', '$account_status', '$fp32_support', '$fp32_renew', $fp32_install)";
If I use in the query $fp32_install a null value goes in just fine but a date goes in as 0000-00-00, if I use '$fp32_install' the date goes in fine but a NULL value goes in as 1970-01-01 (probably because it sees 'NULL' as a string)
If I echo $fp32_install the value is shown as 2012-08-16 and the SQL type for the column is date and the default is NULL
If you are using posted values from a form, then $_POST['value'] will not be NULL.
You should check for empty values instead.
if($_POST['value']=="")
{
$value="NULL";
}
else
{
$value="'".date("Y-m-d",strtotime($_POST['value']))."'";
}
From the behavior you describe, it sounds as if your DATE column is defined with a DEFAULT 0 clause, or you are providing an invalid value.
According to the MySQL documentation:
<snip>
Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').
</snip>
It's difficult to diagnose the exact problem without seeing example code. As a starter, I suggest you try echoing out the SQL statement that is being sent to the database.
I have a strong suspicion that the value for the DATE column is going to appear with quotes around it, a string value of 'NULL', rather than the bare keyword NULL.
What is the difference between value = NULL; and value = ""; in PHP ?
Am facing wierd response as if I set value = ""; than I get empty array response from database which is what am supposed to get but if I set value = NULL; than I get empty string response from mysql database instead of empty array response.
I am not sure why this is the case. Any Suggestions !!!
Have a look at
Working with NULL Values
The NULL value can be surprising until
you get used to it. Conceptually, NULL
means “a missing unknown value” and it
is treated somewhat differently from
other values. To test for NULL, you
cannot use the arithmetic comparison
operators such as =, <, or <>. Use the IS NULL and IS NOT NULL operators instead.
empty strings and NULL are treated differently in MySQL. I had a similar question about this issue:
why use NOT NULL default = ''?
You have 2 solutions here:
Set the database field(s) you are checking to NOT NULL default '' or something similar.
rewrite your queries so that check both cases. ex: 'where field = '' OR field IS NULL'
Value = NULL sets the variable to nothing, basically. Whereas Value = "" sets it to a blank string and while a blank string might look like nothing, it technically is a value of sorts. NULL and "" are both different values.