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 Link to comment https://forums.phpfreaks.com/topic/77697-pass-an-array-to-a-quyery-string/ 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}'"); Link to comment https://forums.phpfreaks.com/topic/77697-pass-an-array-to-a-quyery-string/#findComment-393333 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) . "')"; ?> Link to comment https://forums.phpfreaks.com/topic/77697-pass-an-array-to-a-quyery-string/#findComment-393338 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 Link to comment https://forums.phpfreaks.com/topic/77697-pass-an-array-to-a-quyery-string/#findComment-393754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.