PHP while 循环与关联数组的问题
我有两个 while 循环填充关联数组——我可以一次运行一个(即注释掉另一个),但是,当我同时运行这两个循环时,我收到服务器错误。尽管查询没有查询同一组数据,但我仍然尝试添加指针重置,但没有成功。这是两个循环:
$thecurrent = array();
$getusers = "SELECT score, uid, like_id FROM wetique_scores GROUP BY like_id;";
$gotusers = mysql_query($getusers,$con);
while ($row = mysql_fetch_array($gotusers))
{
$score = $row['score'];
$userid = $row['uid'];
$likeid = $row['like_id'];
$comboid = $userid.$likeid;
$thecurrent[$comboid] = $score;
}
// 2. 从数据库计算新分数并创建数组 $new
$new = array();
$getusers2 = "SELECT like_id, sum(friend_rating), uid from likes l, friendships f WHERE l.friend_id = f.friend_id GROUP BY l.like_id;";
$gotusers2 = mysql_query($getusers2,$con);
while ($rowb = mysql_fetch_array($gotusers2))
{
$scoreb = $rowb['sum(friend_rating)'];
$useridb = $rowb['uid'];
$likeidb = $rowb['like_id'];
$comboidb = $useridb.$likeidb;
$new[$comboidb] = $scoreb;
}
I have two while loops populating associative arrays -- I can run one at a time (ie commenting the other out) however, when I run both at the same time I receive a server error. Despite the queries not querying the same set of data I've still tried adding a pointer reset with no luck. Here are the two loops:
$thecurrent = array();
$getusers = "SELECT score, uid, like_id FROM wetique_scores GROUP BY like_id;";
$gotusers = mysql_query($getusers,$con);
while ($row = mysql_fetch_array($gotusers))
{
$score = $row['score'];
$userid = $row['uid'];
$likeid = $row['like_id'];
$comboid = $userid.$likeid;
$thecurrent[$comboid] = $score;
}
// 2. calculate new score from db and create array $new
$new = array();
$getusers2 = "SELECT like_id, sum(friend_rating), uid from likes l, friendships f WHERE l.friend_id = f.friend_id GROUP BY l.like_id;";
$gotusers2 = mysql_query($getusers2,$con);
while ($rowb = mysql_fetch_array($gotusers2))
{
$scoreb = $rowb['sum(friend_rating)'];
$useridb = $rowb['uid'];
$likeidb = $rowb['like_id'];
$comboidb = $useridb.$likeidb;
$new[$comboidb] = $scoreb;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询的选定字段中没有
'l_id'
,因此$rowb['l_id']
或$row['l_id']< /code> 将生成通知。
来自 l, fips f
我希望这是拼写错误吗?您的
GROUP BY l_id
不清楚。具有相同l_id
的所有行都具有相同的score、uid、like_id
?很奇怪。每次循环后使用
mysql_free_result
https://www.php.net/manual/en/function.mysql-free-result.phpThere is no
'l_id'
in selected fields of your queries, so$rowb['l_id']
or$row['l_id']
will generate Notice.from l, fips f
this is typo i hope?Your
GROUP BY l_id
isn't clear. All rows with samel_id
has samescore, uid, like_id
? Very strange.Use
mysql_free_result
after each cycle https://www.php.net/manual/en/function.mysql-free-result.php