Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/12/2015, 07:27
orcajavi
 
Fecha de Ingreso: febrero-2009
Mensajes: 44
Antigüedad: 15 años, 2 meses
Puntos: 1
Hacer dos consultas mysql seguidas en php

Hola a todos, tengo un código de una consulta mysql que funciona perfectamente en phpmyadmin, es la siguiente:

Código:
SET @numero=0;
select @numero:=@numero+1 AS `posicion`, tot.idpiloto, tot.nombre_piloto, tot.temporadas, tot.puntos puntos, tot.coi, tot.moe , tot.mt mt, round((puntos/mt), 2) media_puntos, tot.victorias, tot.podiums, tot.vr, tot.pm, tot.mp, tot.pp, tot.primero, tot.segundo, tot.tercero from
(
SELECT idpiloto, nombre_piloto, count(idtemporada) temporadas, sum(puntos) puntos, sum(coi) coi, sum(moe) moe, sum(mt) mt, sum(victorias) victorias, sum(podiums) podiums, sum(vr) vr, round(avg(pm)) pm, min(mp) mp, max(pp) pp, sum(primero) primero, sum(segundo) segundo, sum(tercero) tercero
from resultados
group by idpiloto
order by sum(puntos) desc, sum(victorias) desc)tot
La primera linea me pone el contador a 0 y luego, a partir de la segunda línea, me genera una lista de clasificación numerada.

El problema lo tengo al pasar a php. Tengo el siguiente código y solo me funciona bien si quito la primera linea de mysql (SET @numero=0;) pero claro, al quitar la primera linea, no me numera las filas.

Código PHP:
<?php


$sConsulta 
= <<<CONSULTA
SET @numero=0;
select @numero:=@numero+1 AS `posicion`, tot.idpiloto, tot.nombre_piloto, tot.temporadas, tot.puntos puntos, tot.coi, tot.moe , tot.mt mt, round((puntos/mt), 2) media_puntos, tot.victorias, tot.podiums, tot.vr, tot.pm, tot.mp, tot.pp, tot.primero, tot.segundo, tot.tercero from
(
SELECT idpiloto, nombre_piloto, count(idtemporada) temporadas, sum(puntos) puntos, sum(coi) coi, sum(moe) moe, sum(mt) mt, sum(victorias) victorias, sum(podiums) podiums, sum(vr) vr, round(avg(pm)) pm, min(mp) mp, max(pp) pp, sum(primero) primero, sum(segundo) segundo, sum(tercero) tercero
from resultados
group by idpiloto
order by sum(puntos) desc, sum(victorias) desc)tot
CONSULTA;
require (
'../../conexion_base_datos/datos_conexion.php');
$conexion = new mysqli($hostname$username$password$database);
if (
$conexion == FALSE){
echo(
'Error en la conexión.');
exit();
}

$resultado $conexion->query($sConsulta);
if(
$resultado == FALSE){
echo(
'Error en la consulta.');
echo(
$conexion->error);
exit();
}

        echo 
'<div class="seccion3" style="width: 1000px; margin-left: 5%;">';
        echo 
'<table style="text-align: center;"><tr>';
        echo 
'<td width="50px">Pos.</td>';
        echo 
'<td width="200px">Piloto</td>';
        echo 
'<td width="50px">Temp.</td>';
        echo 
'<td width="80px">Puntos</td>';        
        echo 
'<td width="50px">C.O.I.</td>';
        echo 
'<td width="50px">M.O.E.</td>';
        echo 
'<td width="50px">M.T.</td>';
        echo 
'<td width="50px">Media Ptos.</td>';
        echo 
'<td width="50px">Vict.</td>';
        echo 
'<td width="50px">Pod.</td>';
        echo 
'<td width="50px">V.R.</td>';
        echo 
'<td width="50px">P.M.</td>';
        echo 
'<td width="50px">M.P.</td>';
        echo 
'<td width="50px">P.P.</td>';
        echo 
'<td width="50px">1º</td>';
        echo 
'<td width="50px">2º</td>';
        echo 
'<td width="50px">3º</td></tr>';
        
            while(
$registro mysqli_fetch_assoc($resultado)){
                
            echo 
'<tr class="tr_estadisticas">';
            echo 
'<td> </td>';
            echo 
'<td style="text-align: left; padding-left: 5px;">'.$registro['nombre_piloto'].'</td>';
            echo 
'<td>'.$registro['temporadas'].'</td>';
            echo 
'<td>'.$registro['puntos'].'</td>';
            echo 
'<td>'.$registro['coi'].'</td>';
            echo 
'<td>'.$registro['moe'].'</td>';
            echo 
'<td>'.$registro['mt'].'</td>';
            echo 
'<td>'.$registro['media_puntos'].'</td>';
            echo 
'<td>'.$registro['victorias'].'</td>';
            echo 
'<td>'.$registro['podiums'].'</td>';
            echo 
'<td>'.$registro['vr'].'</td>';
            echo 
'<td>'.$registro['pm'].'</td>';
            echo 
'<td>'.$registro['mp'].'</td>';
            echo 
'<td>'.$registro['pp'].'</td>';
            echo 
'<td>'.$registro['primero'].'</td>';
            echo 
'<td>'.$registro['segundo'].'</td>';
            echo 
'<td>'.$registro['tercero'].'</td></tr>';
                
            }
        echo 
'</table>';
        echo 
'</div>';
        
$conexion->close();


?>
Este es el error que me da:
Cita:
Error en la consulta.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select @numero:=@numero+1 AS `posicion`, tot.idpiloto, tot.nombre_piloto, tot.te' at line 2
¿Qué hago mal?

Un saludo