问题描述:

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 newb, so maybe I'm not even going about doing this the right way.

网友答案:

try this

$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
if(mysql_num_rows($result1)<5)
{
while ($row = mysql_fetch_array($result1))
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
}
网友答案:
$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);
}

Use { } for variables in " " ... and why you are not using joins ?

相关阅读:
Top