simon551 Posted July 21, 2007 Share Posted July 21, 2007 Hi, Can you use an array as a criteria in a new query? just wondering if it is possible to do something like this: $queryA="SELECT * FROM table1 WHERE x=1"; $resultA=mysql_qry($queryA); while ($array=mysql_fetch_array($resultA)) { $queryB="SELECT * FROM table2 WHERE y IN($array)"; } Thanks in advance, -s Link to comment https://forums.phpfreaks.com/topic/61143-query-array-array-variable-query-variable-as-criteria/ Share on other sites More sharing options...
marcus Posted July 21, 2007 Share Posted July 21, 2007 You would have to give "$array" a field parameter to work. WHERE y IN($array[x]) Link to comment https://forums.phpfreaks.com/topic/61143-query-array-array-variable-query-variable-as-criteria/#findComment-304269 Share on other sites More sharing options...
simon551 Posted July 21, 2007 Author Share Posted July 21, 2007 Thanks! That works. What if I wanted to use this in a page without writing the whole query in to use it inline. I would normally keep my queries in a separate page. This works: $queryA="SELECT * FROM table1 WHERE x=1"; $resultA=mysql_qry($queryA); while ($array=mysql_fetch_array($resultA)) { $queryB="SELECT * FROM table2 WHERE y IN($array[x])"; do { echo $row['y_1'].'<br />; } while ($row=mysql_fetch_assoc($queryB)); } } But feels like it could be a bit much although I'm too new to really know. This doesn't work but I'm not sure why: $queryA="SELECT * FROM table1 WHERE x=1"; $resultA=mysql_qry($queryA); while ($array=mysql_fetch_array($resultA)) { $queryB="SELECT * FROM table2 WHERE y IN($array[x])"; } do { echo $row['y_1'].'<br />; } while ($row=mysql_fetch_assoc($queryB)); I realize your getting away from the while array logic, but it would be much cleaner to be able to write it like this in the page ( just the echo part without the query.) Link to comment https://forums.phpfreaks.com/topic/61143-query-array-array-variable-query-variable-as-criteria/#findComment-304322 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.