This question already has answers here:
Querying MySQL with IN clause using PHP [duplicate]
(4 answers)
php mysql IN clause not working with CSV variable. only first row is affected
(1 answer)
Laravel Eloquent "WHERE NOT IN"
(12 answers)
Closed 3 years ago.
Currently I have this variable
$arrays = implode(", ", $request->pmChck);
and If I try to return this variable. I will be getting this kind of output
2019-100,2018-100
As you can see the values are separated by commas
Now in my laravel query, I'm trying to get all the records of the employee except for these two company_id above.
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
The query is not working, it shows all the data and also the data with the company_id of 2019-100 and 2018-100
It should listing all the data except for these two company_id 2019-100 and 2018-100
Is there anything wrong with my format or syntax?
Assuming $request->pmChck is an array of companies you want to exclude, the query will be :
DB::table(..)->select(..)->whereNotIn('company_id', $request->pmChck)->get();
Your imploded array looks like
2019-100, 2018-100
and so is query:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100, 2018-100')
try imploding like this:
implode("', '", $arr)
And query will be ok:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100', '2018-100')
The problem in your query are the single quotes in the IN part.
If you change from
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
To
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN(".$arrays.")");
it should work as you expect it. It currently does not work, because your query would be like the following:
SELECT * FROM table WHERE company_id IN ('123, 234')
which would treat your input values as a single value, 123, 234
Related
This question already has answers here:
Can I bind an array to an IN() condition in a PDO query?
(23 answers)
Closed 12 months ago.
I've searched for the answer online, if I've missed something obvious, I would appreciate links. Otherwise, I'd be grateful for direct help. This is the first time I've ever tried a query like this.
I have the following query:
SELECT * FROM `dice_t` WHERE qty IN (:qty) AND opacity IN (:opacity) AND color IN (:color)
To which I am feeding the following array:
Array
(
[qty] => 1,2
[opacity] => 3
[color] => 467,1007
)
It works perfectly (retrieves 163 rows) in phpMyAdmin (when I type in the values), but in my script, it retrieves only 114 rows, which corresponds to it using only the first value in each field (i.e. qty: 1; opacity: 3; color: 467). I have verified this by running the query with only those values in phpMyAdmin.
My code looks like this:
$statement = $dbConn->prepare($sql);
$statement->execute($queryData);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
When I print the values of $sql and $queryData I get the values listed in the first two code blocks above.
The fields are all integers. I tried searching with single quotes around the values, but got an error.
I can't figure out what I'm doing wrong.
the Varable :qty is handled as Strinf So you have '1,1'
So you must use FIND_IN_SET
SELECT * FROM `dice_t` WHERE FIND_IN_SET(`qty`,:qty) AND FIND_ON_SET(`opacity`,:opacity) AND FIND_IN_SET(`color`,:color)
other ways are in this thread Can I bind an array to an IN() condition?
This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
MySQL, PHP: Select * from table where id is not in array
(3 answers)
Select all field where field value not in array
(3 answers)
Closed 3 years ago.
I am writing an SQL query. I have an array of unknown length and I want to select the data fromMySQL by using that array in the WHERE clause of the query. This is my query right now and it is working fine
$sql = "SELECT DISTINCT messagesutou.SenderID from messagesutou where (messagesutou.SenderID !='$items[1]' AND messagesutou.SenderID !='$items[0]' AND messagesutou.SenderID !='$items[2]') AND messagesutou.RecieverID='$uid'";
But in this I know the length of array ( 3) and I just used the array name with index for testing purpose. Now i want to know if array length is unknown then how would I write this query?
$list = implode(',', $items);
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE 0 = FIND_IN_SET(SenderID, '$list')
AND RecieverID='$uid'
or (taken from Jens's answer which was deleted by him)
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
The difference - both variants are applicable when SenderID and $items values have a numeric type, only the former when they have string type, none when they have string type and contain commas or ticks.
But the latter may be adapted:
$list = '\''.implode('\',\'', $items).'\'';
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
It now acccepts any datatype and allows commas (but not ticks - they must be quoted before imploding).
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I'm using a very simple code to extract data from a MySQL DB to a CSV file.
The code didn't provide the result I expected so I made the code including the query in the CSV file.
In a fist part of the code seemes that the variable containing the query result doesn't actually contain any value, but in another part of the code the variable contains the correct value.
In short, the same variable seems to contain two values in different part of the code.
$sql="SELECT destinazione AS dest,ndc AS n FROM npitz";
$query = mysql_query($sql);
$q="ERROR";
while($row = mysql_fetch_array($query)) {
$query="DELETE FROM npitz_reduced_tmp WHERE
destinazione='".$row['dest']."' AND
ndc LIKE CONCAT('".$row['n']."','%') AND
ndc NOT LIKE '".$row['n']."'";
if($row['n']='77') $q=$query." - ".$row['n'];
mysql_query($query);
}
The variable $row['n'] should contain the result of the SQL query.
After the while loop the variable $q is:
DELETE FROM npitz_reduced_tmp WHERE destinazione='UNITED STATES' AND ndc LIKE CONCAT('','%') AND ndc NOT LIKE '' - 77
The question is: if in the IF statement the value of $row['n'] is '77' why it isn't the same in the $query variable assignment?
You probably should use == instead of = in the if statement
This question already has answers here:
MySQL query String contains
(8 answers)
Closed 7 years ago.
I tried using LIKE and CONTAINS, but neither worked
My column named Column in my table contains "asdl, test3, asdklj, dlksj" and I want to find "test3" in there using this string "test2, test3, test4"? How can I do this?
$string = "test2, test3, test4";
"SELECT * FROM table WHERE CONTAINS(Column, ".$string.")"
Another option is to use LIKE, but doesn't work either:
$string = "test2, test3, test4";
"SELECT * FROM table WHERE Column LIKE '%".$string."%'"
**
In summary I need to find a word in $string that matches with a word in Column.
**
Can be done using like. If your strings are comma seperated values then do it like this:
SELECT
* FROM table
WHERE
column LIKE "test3,%" OR
column LIKE "%,test3,%" OR
column LIKE "%,test3"
Hi There I'm trying to get some data with this SELECT statement and when I just select two items it gives me result but when I place third item it doesn't give any result.
$Query="SELECT * from tableName WHERE status='true' AND gid='".$gid."' AND section='".$cid."'";
Plz any solution.
this one works fine, but when I add third item status='true'. doesn't work.
$Query="SELECT * from tableName WHERE gid='".$gid."' AND section='".$cid."'";
First, let me say this: Double-quoted strings can parse your variables, so this line can work, too:
$Query="SELECT * from tableName WHERE gid='$gid' AND SECTION='$cid'";
Try to learn PHP basics about using single ' and double " quotes here: What is the difference between single-quoted and double-quoted strings in PHP?
Related to the database query, is status field is present in your database table? If not, it should NOT be included within the database, or it will return FALSE boolean value. Instead, use IF if you want to be 'selectively' filtering the status of the table.
if('your conditions here'){
$query = "SELECT * FROM tableName WHERE gid='$gid' AND section='$cid'";
}
I think your mistake is the status='true'
probable the database control its field with 1 or 0 value.