Mysql search for a string in an Array - php

Data stored in the database as below where category is column name and comma separated strings are values.
category = 'cat,cat1,cat2,cat3,cat33,cat4';
I'm trying to search with a category from the list:
$query= mysqli_query($mysqli, "SELECT * FROM table where
INSTR(category,'cat3') > 1 ");
But this doesn't work because the result includes cat3 and cat33.
Any thoughts on how I can do this search, please?
Thanks!

You can use FIND_IN_SET function :
SELECT * FROM table where FIND_IN_SET('cat3', category) > 0
But you should normalize your data by creating category table and relate to your table's primary key

"SELECT * FROM table where category LIKE '%cat3,%'";
"SELECT * FROM table where category LIKE '%cat33,%'";
if you don't want to add comma to the end of each category name, then
"SELECT * FROM table where category LIKE '%cat3,%' OR category LIKE '%cat3'";

You can "solve" this using the query below. BUT I HARDLY RECCOMEND you to normalize your database and use this only if you REALLY NEED
SELECT * FROM table WHERE fild LIKE '%,camp3,%' OR fild LIKE 'camp3' OR field LIKE '%,camp3'

Related

I need a Query with 2 dynamic values

i have an array where i can get id's from a database table, now i need to extract those values as i run a domain link array such as: domain.com/page?id=1
Now this page will bring a category, lets say A category with ID 1 i need to list items under this category on the same database table example products, i can use it without a filter now i need a filter using arrays, here is my code, thank you in advance.
$query ="SELECT * FROM tablename WHERE id = '".$id."' DESC";
before this i have on my php code:
$id = $_GET["id"];
So when i call from this table all comes out, but i need to filter where category 1 (example) have items, not all items with category 2, 3, 4 etc.
With my code i can get the item id as the link with array works fine for example: domain.com/page.php?id=1, but here is the catch, i get item id 1 and i need let's say category id 2, if i run the same link with id=2 i get item id 2 and that's not what i want.
I need to retrieve those values as arrays, any idea? Thank you in advance!
EDITED
Table structure example:
Items
ID, Name, Category
I need Category from Items as a filter like this: domain.com/?id=2&category=2
So i get all items under row Category only
Update your URI to something like this domain.com/page.php?id=1&category=2
$category = $_GET["category"];
$query ="SELECT * FROM tablename WHERE id = '".$id."' AND `Category` = '".$category."'";
Pass the query string parameter optionally
domain.com/page.php?id=1&category=2
Add the where clause dynamically like this
$query ="";
$conact =" where 1=1 ";
if(isset($_GET['id']))
{
$conact.=" and id=".$_GET['id'];
}
if(isset($_GET['category']))
{
$conact.=" and category=".$_GET['category'];
}
$query ="SELECT * FROM tablename $conact DESC";

Check if specific value exists in mysql column

I have mysql column called categories. It can contain single or multiple values like this: 1 or 2 or 1,2,3 or 2,12...
I try to get all rows containing value 2.
$query = "SELECT * FROM my_table WHERE categories LIKE '2'";
$rows = mysql_query($query);
This returns row if column only has value 2 but not 1,2,3 or 2,12. How I can get all rows including value 2?
You can use either of the following:
% is a wildcard so it will match 2 or 1,2, etc. Anything on either side of a 2. The problem is it could match 21, 22, etc.
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
Instead you should consider the find_in_set mysql function which expects a comma separated list for the value.
$query = "SELECT * FROM my_table WHERE find_in_set('2', `categories`)";
Like #jitendrapurohut said, you can do it using
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
$rows = mysql_query($query);
But is really bad to store collections like this. A better aproach is as follow:
categories(id_c, name) => A table with each category
my_table(id_m [, ...])
categories_my_table(id_c, id_m)
Then use this query:
SELECT *
FROM my_table m
INNER JOIN categories_my_table cm ON m.id_m = cm.id_m
INNER JOIN categories c ON cm.id_c = c.id_c
WHERE
c.id_c = 2;
EDIT:
#e4c5 link explains why it is bad to store collections like this...
SELECT * FROM my_table WHERE categories LIKE '%2%' AND categories!='1,2,3' AND categories!='2,12';

2 or 3 Categorys search checkbox php

Each Article can have unlimited categories.
Categories are saved in the database like this
example article_category: '1','3','8'
OR
example article_category: '2','3'
So when I search for a listing under, lets say category 3
$category_id = '1,3';
$SQL = "SELECT * FROM ARTICLES WHERE article_category = '$category_id'";
If it were just one the above would work fine.
I hate to say it but I'm completely lost.. Would I use IN ?
You can write like
$SQL = "SELECT * FROM ARTICLES WHERE article_category IN (".$category_id.")";
You can use FIND_IN_SET() like this:
SELECT *
FROM ARTICLES
WHERE FIND_IN_SET($category_id, article_category) > 0
If I understood your problem then you want to make query for different categories at a time.
If I am correct then to do so you will have to get the categories type in an array or diifferent variables then compare each one of them in the where clause using or.
Like: where article_category='$x1' or article_category='$x2' or... so on.
Use find_in_set();
$SQL= "SELECT * FROM ARTICLES WHERE FIND_IN_SET(1,article_category)>0";

Get products by multiple category id

I'm trying to do this kind of query,
When i make the query by get, i want to display the results from the id, for example if i have this id in my GET =11;
I want to display all the products with the category_id = 11, even if they have another id in the concat format, I don't know how i can do this kind of query,
$id = $_GET['id'];
$sql = "SELECT * FROM `product` WHERE `product_category_id`='$id'";
My product_category_id contains the id's of the categories in CONCAT format:
product_category_id (type text)
11,30,29
The FIND_IN_SET MySQL function might be what you're looking for.
$sql = "SELECT * FROM `product` WHERE FIND_IN_SET('$id', `product_category_id`)";
With a field like that, you will have to do something like this:
$sql = "SELECT *
FROM product
WHERE product_category_id LIKE '$id,%'
OR product_category_id LIKE '%,$id,%'
OR product_category_id LIKE '%,$id'";
In general, though, this will be a slow query.

Can I remove an apostrophe from an SQL search (WHERE clause)?

I have a query in SQL (Mysql) using a where clause.
SELECT * FROM TABLE WHERE name = 'Bristols';
Now I know that there's a row in the table containing Bristol's with an apostrophe, but not one without an apostrophe. However I want to return the row anyway. The problem is that I can only feed the query a value without an apostrophe: Bristols - is there any way within the query to remove the apostrophe from the field the query is searching?
SELECT * FROM TABLE
WHERE replace(name, '''', '') = 'Bristols'
There are several ways to accomplish this:
See Fiddle
Regex:
SELECT *
FROM cities
WHERE name REGEXP 'Bristol\'?s';
Replace:
SELECT *
FROM cities
WHERE 'Bristols' = replace(name,'\'','');
Explicit Matching:
SELECT *
FROM cities
WHERE name IN('Bristols','Bristol''s');
You have Two possible outlooks:
First:
SELECT * FROM TABLE WHERE name LIKE '%Bristol%' // Gather data like: BrISTOLS, Bristols, Bristol, Bristol's,
Second:
SELECT * FROM TABLE WHERE replace(name,'''','') = 'Bristols'

Categories