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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.