Column concatenation returns "Null" Mysql - Php - php

I can able to concatenate the values using the following code
$sqlselect = "UPDATE billing_details SET SRF = CONCAT(Year, ID)";
but it returns the result value as "NULL". Kindly help me to solve this issue
Table Structure:
Year *Varchar(5)*
ID *Int(10)*
SRF *Varchar(100)*
Result Table:
Year Id SRF
A 1 NULL
A 2 NULL
A 3 NULL

MySql CONCAT function is return NULL if any argument value if null value, see blow
SELECT COCAT(NULL, 1)
OR
SELECT COCAT(1, NULL)
>NULL
If you want to keep other values while any argument value have null then use IFNULL() function to exclude NULL values
In your query
$sqlselect = "UPDATE billing_details SET SRF = CONCAT(IFNULL(Year, ''), IFNULL(ID, ''))";

Related

How to ignore null value while fetching the data from database table php mysql

How to ignore null value while fetching the data from database table php mysql. Anyone please help me
My mysql query is :
$sql = "SELECT DISTINCT cuid FROM temp_donor_list WHERE donor_no='$name'";
Try this below code
$sql = "SELECT DISTINCT cuid FROM temp_donor_list WHERE donor_no='$name' and cuid!='' and donor_no!=''";
If your field contains NULL value then you can use IS NOT NULL condtion.
If your field contains blank string then you can use <> opertor
SELECT DISTINCT cuid FROM temp_donor_list WHERE donor_no='$name' AND cuid IS NOT NULL AND cuid<>''

MySQL Where clause values 0 and 1 not working correctly

The situation
In table I have columns configurated as ENUM('0','1'). I have select query build with PDO like this example
$value = isset($_POST['value']) ? $_POST['value'] : (isset($_GET["value"]) ? $_GET["value"] : null);
$sql = $pdo->prepare("SELECT * FROM tablename WHERE column = :value");
$sql->bindValue(':value', $_POST['value']); // post contains 0 or 1
$sql->execute();
The problem
When printing the results, value 1 is working normally. But when using value 0, all rows are showing including rows with value 1.
Following query is working normally when trying it in HeidiSQL, but it's not with PHP. What's wrong? SELECT * FROM tablename WHERE column = '0'
I noticed that PHP thinks $_POST['value'] is unset when its value is zero. I'm using isset()
Trying to solve the problem
No effect either if using $_GET['value'] and url like index.php?value=0
Tried following, not working $sql->bindValue(':value', '0'); // post contains 0 or 1
I changed column type to TINYINT(1) - no effect. When looking for zero, all are showing.
Set PDO bindValue() $data_type to PDO::PARAM_BOOL, not working
Try this:
$value = empty($_POST['value']) ? '0' : '1' ;
$sql = $pdo->prepare("SELECT * FROM tablename WHERE column = :value");
$sql->bindValue(':value', $value, PDO::PARAM_STR); // post contains 0 or 1
$sql->execute();
Good luck!
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.
An enumeration value must be a quoted string literal; it may not be an expression, even one that evaluates to a string value.
The index value of the empty string error value is 0. This means that
you can use the following SELECT statement to find rows into which
invalid ENUM values were assigned:
mysql> SELECT * FROM tbl_name WHERE enum_col=0;
So use single quote to consider the zero(0) as string.

SQL checking for NULL fields

SOLVED: I knew there were fields that were empty that should have caught the check, but they were empty rather than NULL. So, when I was checking for NULL fields, it didn't find any.
I'm trying to check whether elements in a row with a specific name have any blank fields. I use it to find if any values are null, if there is, update and rows that were null. It isn't working properly though. I believe I have all the column names correct, I can't see what the problem is though. Here is my query:
//this goes through each row 1 by 1 and checks if an element is null
$stmt = $dbh->prepare("SELECT count(*) from csgo_item_list WHERE Item_Name =:itemname AND (Image_Link IS NULL OR Quantity IS NULL OR new_price IS NULL OR market_hash_name IS NULL OR last_update IS NULL OR is_tradable IS NULL OR old_price IS NULL OR item_type IS NULL OR skin_quality IS NULL OR skin_color IS NULL)");
//"$mydata->market_name" returns a valid name in the table
$stmt->bindValue(':itemname', $mydata->market_name);
$stmt->execute();
$count = $stmt->fetchColumn();
echo $count;
When I do this, some of the rows do have some null fields, yet when I echo $count it returns only 0's. This means I can't update my rows, because after the check I use the same line for an UPDATE:
if($count != 0){
$sql = $dbh->prepare("UPDATE csgo_item_list SET Quantity=:quantity, new_price=:newprice, Image_Link=:image_link, market_hash_name=:markethashname, last_update='1000-01-01 00:00:00', is_tradable='no', old_price=:oldprice, item_type=:type, , skin_quality=:skinquality, skin_color=:skincolor WHERE Item_Name=:itemname AND (Image_Link IS NULL OR Quantity IS NULL OR new_price IS NULL OR market_hash_name IS NULL OR last_update IS NULL OR is_tradable IS NULL OR old_price IS NULL OR item_type IS NULL OR skin_quality IS NULL OR skin_color IS NULL)");
I'll post an image of my database table, from what I've checked the names all match though:
http://gyazo.com/5ab3f2676c44eb696b02a38a64d9742a
Can anyone see why this isn't working?
I knew there were fields that were empty that should have caught the check, but they were empty rather than NULL. So, when I was checking for NULL fields, it didn't find any.
What I had to do was set all the empty columns to NULL:
UPDATE `table` SET `column` = NULL;
"....yet when I echo $count it returns only 0's. This mea...."
if($count != 0){ ....
if it is returning all '0's then why you are comparing "not equal" to 0 ?
it Should be if ($count == O){ I think.

Update column if received x otherwise update y

If I have a table with 3 columns: id,column1,column2. If i want to update column1 just when receiving "column1" parameter in URL request otherwise update column2 when receiving "column2" parameter in URL adress. Is that possible? I made that but i think that's not correct:
$sql= "UPDATE people SET
answer_yes= '$answer_yes'+1,
answer_no='$answer_no'+1";
Thank you for helping.
EDIT: Now that is working (based on Richard Vivian answer)
If($answer_yes==1)
{
$sql= "UPDATE people SET answer_yes= answer_yes +1"or die(mysql_error());
mysql_query($sql);
}
else if ($answer_no==0)
{
$sql= "UPDATE people SET answer_no= answer_no+1" or die(mysql_error());
mysql_query($sql);
}
You can create 2 SQL statement options:
If($answer_yes)
{
$sql= "UPDATE people SET answer_yes= '$answer_yes'+1"
}
else
{
$sql= "UPDATE people SET answer_no= '$answer_no'+1"
}
I'm unsure which database model you are using, but the logic would be to pass a NULL value if you don't have a value to pass, and check that the values not null before updating.
SQL Server
UPDATE Table
SET Column1=ISNULL(#Column1,Column1),
Column2=ISNULL(#Column2,Column2)
MySQL
UPDATE Table
SET Column1=IFNULL($Column1,Column1),
Column2=IFNULL($Column2,Column2)
What is happening here is that ISNULL/IFNULL is checking whether the first value passed to it is NULL, and if it is, its returning the 2nd value. The 2nd value is the same value as the current value, and therefore it updates it with the same value (ie. Not changing the value).
You can do this:
UPDATE people
SET answer_yes = COALESCE($answer_yes + 1, answer_yes),
answer_no = COALESCE($answer_no + 1, answer_no);
The COALESCE returns the first non NULLable value in the values passed to it. If any of the parameters $answer_yes or $answer_no were passed with a NULL value, then $answer_yes + 1 and $answer_no + 1 will be evaluated to NULL also, there for the COALESCE will return the column value, and in this case the column will be updated with its value, i.e, it won't changed.

select rows with column that is not null?

by default i have one column in mysql table to be NULL.
i want to select some rows but only if the field value in that column is not NULL.
what is the correct way of typing it?
$query = "SELECT *
FROM names
WHERE id = '$id'
AND name != NULL";
is this correct?
You should use (assuming $id is an integer):
$query = "SELECT *
FROM names
WHERE id = '" . (int) $id ."'
AND name IS NOT NULL";
You must use IS NULL or IS NOT NULL when working with NULL values
AND name IS NOT NULL
(NULL comparisons require the special IS and IS NOT operator in SQL)

Categories