Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: viher en 19 Agosto 2010, 16:28 pm



Título: problema $_GET['id'];
Publicado por: viher en 19 Agosto 2010, 16:28 pm
hola,tengo el siguiente código que me funciona casi a la perfección,es decir yo pongo archivo.php?id=15 y funciona todo bien
Código
  1. <? include ("config.php");
  2. $id = $_GET['id'];
  3. $sql = "select * from peliculas where id=$id";
  4. //Instrucción a ejecutarse en la bbdd.
  5.  
  6. $squery = mysql_query($sql);
  7. //Ejecución de la instrucción
  8.  
  9. while($row = mysql_fetch_array($squery)){
  10. //Recuperar los datos de un registro o hilera (row) y meterlo a un array
  11. echo "<b>".$row['des']."</b>"; }
  12. ?>

el caso es que si hago abro archivo.php me da los siguientes errores:

Código:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/x/public_html/x/x.php on line 128


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/x/public_html/x/x.php on line 153
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 '' at line 1

como podría hacer para que si entran en video.php no diera ese error?

gracias


Título: Re: problema $_GET['id'];
Publicado por: Shell Root en 19 Agosto 2010, 19:56 pm
Si funciona el código correctamente, deberías de verificar el error, en el otro archivo (/home2/x/public_html/x/x.php). Además para estar más seguros, trata de ponerle excepciones. Así:
Código
  1. <?
  2.   include ("config.php");
  3.   $id = $_GET['id'];
  4.   $sql = "select * from peliculas where id=".$id;
  5.  
  6.   $squery = mysql_query($sql);
  7.   if (!$squery ) {
  8.      die('Invalid query: ' . mysql_error());
  9.   }
  10.  
  11.   while($row = mysql_fetch_array($squery)){
  12.      echo "<b>".$row['des']."</b>";
  13.   }
  14. ?>