Variable not showing first row of the query

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 );
            ?>;

Answers


Because you have called mysql_fetch_assoc two times, remove this line

$row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas);

And use only this one

while( $row_qryvalAltnsRotas = mysql_fetch_assoc($qryvalAltnsRotas) )

Need Your Help

Run groovy script code contained in a string

java groovy

I have to implement a java function for my thesis that executes a groovy script and get the result back.