'Get All Records With Single ID From Multiple IDs In PHP

I have a blog page from where I send the category Id to fetch record from a blog table where category id match with the GET value. Like this,

category-blog.php?catid=2

What I want is, that I want to fetch all records from a table in which the Get value ( i.e id ) is present. I have a blog table structure like, enter image description here

Suppose, if my GET value is 2, I want to fetch all the records from the blog table where category_id is 2 present.

Sorry, for my bad English. Please help me out.



Solution 1:[1]

You can use FIND_IN_SET

EXAMPLE

SELECT FIND_IN_SET('ank','b,ank,of,monk');

OUTPUT

mysql> SELECT FIND_IN_SET('ank','b,ank,of,monk'); 
+------------------------------------+
| FIND_IN_SET('ank','b,ank,of,monk') |
+------------------------------------+
|                                  2 | 
+------------------------------------+
1 row in set (0.00 sec)

Your Query

$cat_ids = '2'; // OR $cat_ids = implode(',', array('2', '4', '1'));
// Use REPLACE to remove extra space between , and INT so that 1, 5, 2 becomes 1,5,2
SELECT * FROM table WHERE FIND_IN_SET($cat_ids, REPLACE(category_id, ', ', ','));

HELP LINK FIND_IN_SET()

Solution 2:[2]

Use FIND_IN_SET() for comma separated value in table

SELECT *
 FROM `tablename`
 WHERE FIND_IN_SET('2',`category_id` ) > 0

Hope this helps.

Solution 3:[3]

you must try with this

 SELECT *
 FROM blogs
 WHERE FIND_IN_SET( `category_id`, 2 );

Solution 4:[4]

SELECT * FROM `tableName` WHERE FIND_IN_SET('2',`category_id`)>0 

Solution 5:[5]

This code worked for me in a while loop

$my_ids = $logged_ids; or $my_ids = 2;

$query = mysqli_query($con,"SELECT * FROM campaigns WHERE FIND_IN_SET($my_ids, REPLACE(ids, ', ', ','))");
while($result=mysqli_fetch_assoc($query)){

echo $result["ids"];

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Saty
Solution 4 Toby Speight
Solution 5 Muneem Billah