从同一个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 ",";
        }?>]

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

First of, you should definently change from the mysql_* family of functions, since they are clearly deprecated. The only valid reason not to is that you php installation is outdated and you are not in control of the server (as in it's hosted), and even that is barely acceptable.

I would solve your problem by doing this:

<script>
var time2 = [];
var text = [];
<?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))
  {
    echo "time2.push('".$row['timecode']."');";
    echo "text.push('".$row['tag']."');";
  }
?>
</script>

This allows you to fill up the two variables "at the same time". Well actually you let the client do the work, which I personally do not see a problem with.

Another, perhaps more sensible, way of doing it - which I won't show you - is to make the page do an AJAX request for the data, return all the data in a simple JSON structure and sort thru it on the client...