Using IN() condition in PDO MYSQL [duplicate] - php

This question already has answers here:
Can I bind an array to an IN() condition in a PDO query?
(23 answers)
Closed 7 years ago.
I'm trying to search a value in a mysql table field. using prepared statements.
services: 2,4,5
passed value: 2
$query = "SELECT *
FROM table
WHERE services IN(':array')"
so basically I'm trying to return all rows where 2 is inside the services field. The field itself will have numbers separated by commas. So the services field can look like this: 2,3,6,7,1 or just something like this: 4. Any help is greatly appreciated.

$query = "SELECT *
FROM table
WHERE FIND_IN_SET(':array', services )"
:array is 2 what u want pass.

Related

How to fetch only matched items of a php array from mysql database table [duplicate]

This question already has answers here:
Passing an array to a query using a WHERE clause
(17 answers)
Closed 4 years ago.
This is my array in php: $list = array('dog', 'tiger', 'lion', 'elephant')
Table name: watchlist Database: assignment
After comparing the $list with the watchlist table it should print only the matched items.
So, the output for this will be:
dog
tiger
Is there any mysql query to get this output. I am weak in mysql. Thank you in advance.
You can do this:
$query = "SELECT * FROM `watchlist` WHERE `assignment` IN('".implode("','",$list)."')";
Note: Make sure that any external input is sanitized.

Mysql query to get the comma separated value (1,10,15) stored in a column with WHERE Condtion that also has a comma separated Value (15,1,100) [duplicate]

This question already has answers here:
SQL query to match a comma-separated string against a comma-separated string?
(3 answers)
Closed 5 years ago.
My Table Look like Below, Please refer Screenshot.Table Example Structure
I want a query to get the "id" when I pass the comma separated values (ex:25,10,3) in WHERE Condition of user_id column.
EX: Select id from table where user_id IN('25,10,3')
When I execute the above query I should get ID's 1 and 2.
I tired with "WHERE IN" and "find_in_set()". But nothing worked out...
Please help me to get this done...
Balachander,
find_in_set will work (see SQLFiddle), but you should reconsider using proper database structure.
Finally I have Find a Solution for this....
select * from test where user_id REGEXP '(^|,)(25|10|3)(,|$)'
Refer SQLSQLFiddle

How can I fetch with PDO in order to have names of the columns in the first array position? [duplicate]

This question already has answers here:
php pdo: get the columns name of a table
(14 answers)
Closed 7 years ago.
How would you fetch a mysql query with PDO (Or after PDO) in order to have this result? I mean the "titles of the columns" in the first position of the array and the values in the next positions? Can somebody helping me with an example?
[
["ColumnTitle","Column2Title"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"]
]
The mysql query returns:
ColumnTitle | Column2Title
----------------------------
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
I don't think this question was duplicate, this was a specific case.. :(
PDO doesn't offer such a format out of the box (because no developer would like it this way), but you can make it with minor tweaking
$data = $pdo->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);
array_unshift($data, array_keys($data));
But in your place I wouldn't add column names to the data array at all.

SQL+PHP: how can i return all PHP array existed values from SQL table? [duplicate]

This question already has answers here:
Passing an array to a query using a WHERE clause
(17 answers)
Closed 10 years ago.
Hello All
I have a simple php array, For Example:
$numbers=array("12345","65432","98765");
And SQL Table named: "phones" with the column "TNumbers" and the rows: 654654, 12345, 87878.
Now, What will be the fastest way to return all the values from the array that exist in the SQL server table? (So it should return only this: ("12345"))
Thank you Very Much!!
After some more search i have found the answer, Sorry for that:
$ids = join(',',$numbers);
$sql = "SELECT * FROM phones WHERE TNumbers IN ($ids)";
Thank you anyway!

Retrieve articles from array of category ids [duplicate]

This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query [duplicate]
(8 answers)
Closed 11 months ago.
I have an array of category ids and want to retrieve articles from my mysql database with these category ids. What is the best method for this?
mysql_query('SELECT * FROM articles WHERE category_id IN (\''.implode('\'',array_map('mysql_real_escape_string',$categories)).'\')');
Specify how articles are joined to categories if this is not how your db/table setup.
Look here:
I have an array of integers, how do I use each one in a mysql query (in php)?
for a safe, parameter-based approach and code sample.

Categories