Foros del Web » Programando para Internet » PHP »

Warning: mysql_fetch_array()

Estas en el tema de Warning: mysql_fetch_array() en el foro de PHP en Foros del Web. Hola gente. Tengo el siguiente codigo que me esta genereando este error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\curso\news\noticias.php on ...
  #1 (permalink)  
Antiguo 13/04/2011, 18:16
Avatar de jbriz_gw  
Fecha de Ingreso: marzo-2009
Ubicación: Perez Zeledon
Mensajes: 39
Antigüedad: 15 años, 1 mes
Puntos: 0
Pregunta Warning: mysql_fetch_array()

Hola gente.

Tengo el siguiente codigo que me esta genereando este error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\curso\news\noticias.php on line 8

y el codigo es este

<?php
require_once 'conexion.php';



$idpost = $_GET['id_noticia'];
$post = "SELECT * FROM entradas where id_noticia = $idpost ";
$res = mysql_query($post);
while($row= mysql_fetch_array($res)) {

echo $row["titulo"];


}

?>

el bucle while es el que me genera el error, si hago un echo u anguna otra sentencia me sale bien...
le encuentran algun error?
  #2 (permalink)  
Antiguo 13/04/2011, 18:33
Avatar de bioxido  
Fecha de Ingreso: diciembre-2008
Ubicación: $_SERVER['PHP_SELF']
Mensajes: 601
Antigüedad: 15 años, 5 meses
Puntos: 21
Respuesta: Warning: mysql_fetch_array()

intenta:
$post = "SELECT * FROM `entradas` where id_noticia = '$idpost' ";

Y una recomendacion... En tu caso es mejor usar mysql_fetch_assoc() consume menos recursos que mysql_fetch_array() y es mas rápido... ;)
  #3 (permalink)  
Antiguo 13/04/2011, 18:45
Avatar de jbriz_gw  
Fecha de Ingreso: marzo-2009
Ubicación: Perez Zeledon
Mensajes: 39
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Warning: mysql_fetch_array()

Cita:
Iniciado por bioxido Ver Mensaje
intenta:
$post = "SELECT * FROM `entradas` where id_noticia = '$idpost' ";

Y una recomendacion... En tu caso es mejor usar mysql_fetch_assoc() consume menos recursos que mysql_fetch_array() y es mas rápido... ;)

no me sirvio...no es un problema en llamar a la base de datos, es el while, porque si lo quito y pongo por ejempo
echo $idpost me devuelve lo esperado
  #4 (permalink)  
Antiguo 13/04/2011, 18:53
 
Fecha de Ingreso: marzo-2011
Ubicación: Veracruz
Mensajes: 92
Antigüedad: 13 años, 1 mes
Puntos: 3
Respuesta: Warning: mysql_fetch_array()

el $idpost te lo devuelve porque no tiene que ver con el error que te está marcando, yo uso mysql_fetch_object, intentalo asi:

Código PHP:
Ver original
  1. <?php
  2. require_once 'conexion.php';
  3.  
  4.  
  5.  
  6. $idpost = $_GET['id_noticia'];
  7. $post = "SELECT * FROM entradas where id_noticia = '$idpost' ";
  8. $res = mysql_query($post);
  9. while($row= mysql_fetch_object($res)) {
  10.  
  11. echo $row->titulo;
  12.  
  13.  
  14. }
  15.  
  16. ?>
  #5 (permalink)  
Antiguo 13/04/2011, 18:55
 
Fecha de Ingreso: marzo-2011
Ubicación: Veracruz
Mensajes: 92
Antigüedad: 13 años, 1 mes
Puntos: 3
Respuesta: Warning: mysql_fetch_array()

Ah se me olvidaba decirte que te recomiendo que uses mysql_error() :

Código PHP:
Ver original
  1. $res = mysql_query($post) or die(mysql_error());
  #6 (permalink)  
Antiguo 13/04/2011, 18:58
Avatar de KoswiDigital  
Fecha de Ingreso: abril-2011
Ubicación: Galicia - España
Mensajes: 220
Antigüedad: 13 años
Puntos: 37
Respuesta: Warning: mysql_fetch_array()

Hola.

Fíjate bien que $idpost vaya entre comillas simples como te indica bioxido. El bucle está bien, el problema es que no te devuelve nada porque no lo encuentra. El error que aparece, lo que te dice es que la consulta no generó ningún resultado. Por lo que algo falla, y no es el while.


Un saludo.
__________________
http://www.koswidigital.com - Diseño web, Programación web, Fotografía profesional, Retoque digital.
  #7 (permalink)  
Antiguo 13/04/2011, 19:07
Avatar de jbriz_gw  
Fecha de Ingreso: marzo-2009
Ubicación: Perez Zeledon
Mensajes: 39
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Warning: mysql_fetch_array()

Cita:
Iniciado por gmurop Ver Mensaje
el $idpost te lo devuelve porque no tiene que ver con el error que te está marcando, yo uso mysql_fetch_object, intentalo asi:

Código PHP:
Ver original
  1. <?php
  2. require_once 'conexion.php';
  3.  
  4.  
  5.  
  6. $idpost = $_GET['id_noticia'];
  7. $post = "SELECT * FROM entradas where id_noticia = '$idpost' ";
  8. $res = mysql_query($post);
  9. while($row= mysql_fetch_object($res)) {
  10.  
  11. echo $row->titulo;
  12.  
  13.  
  14. }
  15.  
  16. ?>

Con tu codigo ahora me devuelve este error

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\curso\news\noticias.php on line 10

y con el $res = mysql_query($post) or die(mysql_error()) ;
me dice No database selected
  #8 (permalink)  
Antiguo 13/04/2011, 19:21
Avatar de KoswiDigital  
Fecha de Ingreso: abril-2011
Ubicación: Galicia - España
Mensajes: 220
Antigüedad: 13 años
Puntos: 37
Respuesta: Warning: mysql_fetch_array()

Cita:
No database selected
Pues te está diciendo que no tienes ninguna base de datos seleccionada. Comprueba tu archivo conexion.php y mira que haya un mysql_select_database() en algún sitio...
__________________
http://www.koswidigital.com - Diseño web, Programación web, Fotografía profesional, Retoque digital.
  #9 (permalink)  
Antiguo 13/04/2011, 19:28
Avatar de jbriz_gw  
Fecha de Ingreso: marzo-2009
Ubicación: Perez Zeledon
Mensajes: 39
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Warning: mysql_fetch_array()

Cita:
Iniciado por KoswiDigital Ver Mensaje
Pues te está diciendo que no tienes ninguna base de datos seleccionada. Comprueba tu archivo conexion.php y mira que haya un mysql_select_database() en algún sitio...
Al fin...ya no me da error. aunque no me muestra nada, pero ese debe ser un error menor..

Mil gracias a todos, pero en especial a KoswiDigital
  #10 (permalink)  
Antiguo 14/04/2011, 04:05
Avatar de KoswiDigital  
Fecha de Ingreso: abril-2011
Ubicación: Galicia - España
Mensajes: 220
Antigüedad: 13 años
Puntos: 37
Respuesta: Warning: mysql_fetch_array()

No se merecen menciones especiales, todos ayudamos desinteresadamente :) Saludos.
__________________
http://www.koswidigital.com - Diseño web, Programación web, Fotografía profesional, Retoque digital.

Etiquetas: mysql, mysql_fetch_array();
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:01.