我对PHP和MySQL有点新意。我似乎遇到了一些问题。如何遍历数组,查询数组中的每个值,直到查询满足某个条件为止。在这种情况下,查询返回的行数将小于5。这就是我所拥有的:
$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);
}
问题是,对于每个回显的值,我收到以下警告: mysql_numrows():提供的参数不是有效的MySQL结果资源
就像我说的那样,我是一个新手,所以也许我甚至不会以正确的方式做这件事。
答案 0 :(得分:0)
试试这个
$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); } }
答案 1 :(得分:0)
$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); }
对于“...中的变量使用{}以及为什么不使用连接?