Select Multiple Ids from a table

前端 未结 2 762
萌比男神i
萌比男神i 2020-12-13 15:02

I want to select some id\'s based on url string but with my code it displays only the first. If i write manual the id\'s it works great.

I have a url like this http:

相关标签:
2条回答
  • 2020-12-13 15:54

    When you interpolate

    "select * from info WHERE `id` IN ('$ids')"
    

    with your IDs, you get:

    "select * from info WHERE `id` IN ('1,2,3,4,5')"
    

    ...which treats your set of IDs as a single string instead of a set of integers.

    Get rid of the single-quotes in the IN clause, like this:

    "select * from info WHERE `id` IN ($ids)"
    

    Also, don't forget that you need to check for SQL Injection attacks. Your code is currently very dangerous and at risk of serious data loss or access. Consider what might happen if someone calls your web page with the following URL and your code allowed them to execute multiple statements in a single query:

    http://www.example.com/myfile.php?theurl=1);delete from info;-- 
    
    0 讨论(0)
  • 2020-12-13 16:01

    You can also try FIND_IN_SET() function

    $SQL = "select * from info WHERE FIND_IN_SET(`id`, '$ids')"
    

    OR

    $SQL = "select * from info WHERE `id` IN ($ids)"
    
    0 讨论(0)
提交回复
热议问题