mjahkoh Posted November 17, 2007 Share Posted November 17, 2007 Assuming i had an array ($myarray) which holds maybe (1,2,3) how do i pass it in the query string so i get select hd from mytable where hd in (1,2,3). Please note that the array values are dynamic Thanks Jackson Quote Link to comment Share on other sites More sharing options...
axiom82 Posted November 17, 2007 Share Posted November 17, 2007 if (!empty ($array)){ $where = "WHERE id=" . implode (' OR id=', $array); } $query = mysql_query ("SELECT * FROM table {$where}'"); Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2007 Share Posted November 17, 2007 You nearly said it in your question. <?php $arr = array(1,2,3); $sql = "SELECT * FROM tbl WHERE hd IN('" . implode(",",$arr) . "')"; ?> Quote Link to comment Share on other sites More sharing options...
websiterepairguys Posted November 18, 2007 Share Posted November 18, 2007 You nearly said it in your question. <?php $arr = array(1,2,3); $sql = "SELECT * FROM tbl WHERE hd IN('" . implode(",",$arr) . "')"; ?> Keep in mind, this isn't a secure way of doing it. If the values in the array come from user entered input (in this example, hard to tell), you need to sanitize each value. That means looping through the array, and applying mysql_escape_string (or applicable db version) to each entry. I would create a function for doing this. Read up on SQL injection attacks. Regards, Mark Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.