drcj64241 2013-03-25 16:03
浏览 53

从同一个MySQL Query中填充两个javascript数组

I'm trying to fill those two arrays (time2 and text) from the same query ($result), however only the first array is filled, and I don't understand why...
Note that I'm invoked from the same query, because ORDER BY RAND () functionality is implemented, and when I need to get the values of some array positions, the correct value should appear. And that's why I can't create another identical query.
Is this possible to do?

    //Array Time
    var time2 = [<?php $title=$_GET["title"];

    $result = mysql_query("SELECT tag, round(avg(timecode)) as timecode, group_id
    FROM tags
    WHERE tags.filename = '".$title."'
    GROUP BY group_id, tag
    ORDER BY RAND()");

    while($row = mysql_fetch_array($result))
        {
        $timecode = $row['timecode'];
        print $timecode;
        print ",";
        }?>]

    //Array Text

    var text = [<?php

    while($row = mysql_fetch_array($result))
        {
        $tag = $row['tag'];
        print "'";
        print $tag;
        print "'";
        print ",";
        }?>]
  • 写回答

2条回答 默认 最新

  • dongying3830 2013-03-25 16:08
    关注

    mysql_fetch_array is a cursor based function. It will run to the end of the result set and stop. Calling it again will still return the 'end of result set'.

    .. and it's deprecated.

    You should:

    1. switch to PDO or mysqli
    2. read out the whole result set to a local var and then loop over that to create your javascript
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测