Passing an array to a query using a WHERE clause

后端 未结 18 1018
情歌与酒
情歌与酒 2020-11-21 09:03

Given an array of ids $galleries = array(1,2,5) I want to have a SQL query that uses the values of the array in its WHERE clause like:



        
18条回答
  •  借酒劲吻你
    2020-11-21 09:32

    Besides using the IN query, you have two options to do so as in an IN query there is a risk of an SQL injection vulnerability. You can use looping to get the exact data you want or you can use the query with OR case

    1. SELECT *
          FROM galleries WHERE id=1 or id=2 or id=5;
    
    
    2. $ids = array(1, 2, 5);
       foreach ($ids as $id) {
          $data[] = SELECT *
                        FROM galleries WHERE id= $id;
       }
    

提交回复
热议问题