Jump to content

Loop through array, query each value until certain condition is met


jitsking

Recommended Posts

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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.