PHP while 循环与关联数组的问题

发布于 2024-11-04 15:30:33 字数 966 浏览 3 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-11-11 15:30:33
  1. 您的查询的选定字段中没有 'l_id',因此 $rowb['l_id']$row['l_id']< /code> 将生成通知。


  2. 来自 l, fips f 我希望这是拼写错误吗?

  3. 您的GROUP BY l_id 不清楚。具有相同 l_id 的所有行都具有相同的 score、uid、like_id?很奇怪。

  4. 每次循环后使用mysql_free_result https://www.php.net/manual/en/function.mysql-free-result.php

  1. There is no 'l_id' in selected fields of your queries, so $rowb['l_id'] or $row['l_id'] will generate Notice.

  2. from l, fips f this is typo i hope?

  3. Your GROUP BY l_id isn't clear. All rows with same l_id has same score, uid, like_id? Very strange.

  4. Use mysql_free_result after each cycle https://www.php.net/manual/en/function.mysql-free-result.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文