[英]Loop through array, query each value until certain condition is met
I'm a bit of a newb to PHP and MySQL. 我对PHP和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. 如何遍历数组,查询数组中的每个值,直到查询满足特定条件。在这种情况下,查询返回的行数将少于5。 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 问题是,对于它回显的每个值,我都会收到以下警告:mysql_numrows():提供的参数不是有效的MySQL结果资源
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 ? 对{“ ...中的变量使用{},为什么不使用联接?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.