这是我的代码:
include('connectTwo.html');
$query = "select * from users";
$result = mysql_query($query, $connectTwo);
while($row = mysql_fetch_array($result))
{
echo $row['username'];
$user_id = $row['user_id'];
$profile = getProfile($user_id);
echo $profile;
}
function getProfile($user_id)
{
$query = "select * from info where user_id='$user_id'";
$result = mysql_query($query, $connectTwo);
$row = mysql_fetch_assoc($result);
$profile = $row['profile'];
return $profile;
}
该函数不返回任何内容。我知道查询是正确的,因为我将它移到函数之外并且它运行并为我提供了我正在寻找的值。如果您对我的问题感到困惑,请发表评论。
最佳答案
尝试左连接:
$query = "select a.*,b.profile from users a left join info b on b.user_id=a.user_id";
然后你可以删除getProfile()
函数。
如果你仍然想使用 function getProfile($user_id)
,global $connectTwo
否则你不能使用它,因为它是未定义的。
关于php - 查询循环 php 中的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128800/