jitsking Posted April 1, 2011 Share Posted April 1, 2011 I'm a bit of a newb to PHP and MySQL. I seem to be having an issue with something. How do I loop through an array, querying each value in the array until the query meets a certain condition.. In this case it would be that the number of rows returned from the query is less than five. Here is what I have: $query1="SELECT UserID FROM Users where RefID='$userid'"; $result1=mysql_query($query1); while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5') { echo ($row[0]); echo " "; $sql2 = "SELECT * FROM Users WHERE RefID=$row[0]"; $sql2result = mysql_query($sql2); $sql2querynum = mysql_numrows($sql2result); } Problem is, for every value it echoes out, I get the following warning: mysql_numrows(): supplied argument is not a valid MySQL result resource Like I said, I'm a newbie to PHP to maybe I'm not even going about doing this the right way. Hoping someone can help to point me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/232398-loop-through-array-query-each-value-until-certain-condition-is-met/ Share on other sites More sharing options...
blacknight Posted April 1, 2011 Share Posted April 1, 2011 change $row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5' to $row = mysql_fetch_row($result1, MYSQL_NUM) && $sql2querynum < '5' but your bigger issue is that you havent set $sql2querynum and $sql2querynum = mysql_numrows($sql2result); to $sql2querynum = mysql_num_rows($sql2result); and try that Quote Link to comment https://forums.phpfreaks.com/topic/232398-loop-through-array-query-each-value-until-certain-condition-is-met/#findComment-1195601 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.