2

The idea is to obtain in a javascript variable an array provided by a mysql query. It works fine, but the result of the variable shows minus 1 row than the query.

Below I have provided just the code ref the problem, skipping all of the rest, I suppose isn't needed.

Thanks in advance for help!

The query:

$search_qryvalAltnsRotas = "-1";
if (isset($_GET['search'])) {
  $search_qryvalAltnsRotas = $_GET['search'];
}
mysql_select_db($database_connect, $connect);
$query_qryvalAltnsRotas = sprintf("SELECT altn, destino FROM tblalternativos WHERE destino = %s ORDER BY destino ASC", GetSQLValueString($search_qryvalAltnsRotas, "text"));
$qryvalAltnsRotas = mysql_query($query_qryvalAltnsRotas, $sado_leitor) or die(mysql_error());
$row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas);
$totalRows_qryvalAltnsRotas = mysql_num_rows($qryvalAltnsRotas);

(...) below is the variable (part of a javascript function):

var alternatesq = 
            <?php               
            while( $row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas) ) {
                $alternates[] = $row_qryvalAltnsRotas['altn'];
            }

            echo json_encode( $alternates );
            ?>;
4

1 回答 1

3

因为你已经调用mysql_fetch_assoc了两次,所以去掉这行

$row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas);

并且只使用这个

while( $row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas) )
于 2013-04-19T09:24:14.017 回答