My database record contains column which is N/A value which is null.
But when I retrieve them from mysql database. It shows me N/A value which I don't want to see.
I did a check for that ,but no luck, the checking condition is fail
I used
empty()
is_null()
$my_variable == null
$my_variable == " "
but the check still fail.
Any know how to use PHP to check the value that retrieved from the database?
updated ques:
Name Type Collation Attributes Null Default Extra Actio
4 description text latin1_swedish_ci No None
default is none
I changed it to default NULL and the field can be NULL, then right now I can check for null value.
Related
I have many forms that I am serializing. The data is posted to either an insert.php page or update.php.
If a form input is left blank, I want to insert a NULL value into my database. I'm using mySQL.
It seems like when a form is serialized, any fields that are left blank are sent as value of '' which does NOT insert as NULL into my db.
I can filter out empty inputs before serializing, but this isn't good when a record needs to be updated.
Here's a fiddle of a form that you can at least see that empty inputs are logged as "".
Any advice on how I can set those values to NULL instead?
jsfiddle.net/gabrieleromanato/bynaK/
You can simply at php side check if value is empty use instead null with strlen(...) == 0 condition ( as #PaulSpiegel mentioned in comment of this answer empty will consider that 0 is also empty and will write null to your db ) - http://php.net/manual/en/function.strlen.php
// Example
$value = !strlen($_POST['value']) ? null:$_POST['value'];
// .. save in MySQL
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 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.
I have a if NULL echo "Message" else $value.
There is some JavaScript hiding the input field, so they check it and enter the value.
If they don't check it to enter the field, the db enters 0.00 and doesn't specify NULL so my PHP if statement works.
How do I either set the variable as NULL if its blank, or set NULL in the INSERT statement?
`myTableField` decimal(10,2) default NULL,
Not sure exactly what you're after, but you can run the below if statement to check what value was entered, and set to null accordingly:
if(!is_numeric($fieldvalue) || $fieldvalue==0){
// if the entered value isnt a number (i.e. isnt entered, or invalid) or if the value is zero, sounded like it was your default
$fieldvalue=NULL;
// could also use unset($fieldvalue);
}
If the variable $fieldvalue is set to null (or un-set), it will be inserted as a NULL in your DB according to your field definitions. Make sure your insert statement references the value without ' or " encapsulating figures however (not needed as its a decimal field).
I want to send a value of "NULL" to a column in mySQL.
$ParentEventID = "NULL";
mysql_query("UPDATE events SET
ParentEventID = '$ParentEventID'");
The column keeps defaulting to "0". I have the column set to accept NULL and I can edit the cell with the NULL check box.
I need to not set the default to NULL because it may effect code in other places.
You are on the right track making "NULL" a string. If you need to set it NULL, then remove the qoutes around $ParentEventID.
mysql_query("UPDATE events SET ParentEventID = $ParentEventID");
However, before doing so make sure that the value of $ParentEventID === NULL.