-3

This is my SQL table

enter image description here

when I try to fetch record with PDO using this mysqli query:

SELECT * FROM `TABLE 1` WHERE `tags` = ? LIMIT 25

? is replaced by a $_GET variable.

so for example i want to get results for this query:

SELECT * FROM `TABLE 1` WHERE `tags` = 'a' LIMIT 25

it will load and for other parameters, it does not load. but when I run a sql query in phpmyadmin the query loads without error(Always)! Can someone help me to understand why it is happening?

Mayuresh
  • 70
  • 1
  • 7
  • Storing comma separated lists is an anti-pattern. https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557 Note that there are no rows that satisfy the condition `tags = 'a'`. MySQL does provide `FIND_IN_SET` function... https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set e.g. `SELECT FIND_IN_SET('a','a,b,c')` e.g. `SELECT t.id, t.tags FROM mytable t WHERE FIND_IN_SET('a',t.tags) ORDER BY t.id` – spencer7593 Dec 30 '19 at 16:57
  • Why is this tagged as "pdo"? You state *"...WHERE clause in PHP **Mysqli**"*. < That is a different animal than PDO. – Funk Forty Niner Dec 30 '19 at 17:05
  • @spencer7593 look at the image in my question, you can see the first & 7th column has value 'a' in it. – Mayuresh Dec 30 '19 at 17:17
  • @Dry7 I have added a picture in the answer kindly take a look! – Mayuresh Dec 30 '19 at 17:26
  • @Mayuresh: rows 1 and 7 do NOT satisfy the condition `tags = 'a'`. As a demonstration : `SELECT 'a,b,c' = 'a'` will return 0 (FALSE). We expect that because the two strings are not equal. As I suggested in my previous comment, we can use MySQL `FIND_IN_SET` function, as a demonstration `SELECT FIND_IN_SET('a','a,b,c')` will return 1. Evaluated in boolean context, a nonzero value is considered TRUE. – spencer7593 Dec 30 '19 at 17:35

1 Answers1

0

you can use FIND_IN_SET in your query if you are storing comma-separated values

    SELECT 
        t.id, t.tags 
    FROM mytable t 
    WHERE FIND_IN_SET('a',t.tags) 
    ORDER BY t.id

or else use can use wild card like

        SELECT 
            t.id, t.tags 
        FROM mytable t 
        WHERE t.tags like '%a%') 
        ORDER BY t.id
Abdul Moiz
  • 447
  • 2
  • 10