Jump to content


Photo

Ayuda con esta sentencia mysql


  • Please log in to reply
2 replies to this topic

#1 iosujamed

iosujamed

    Newbie

  • Miembros
  • Pip
  • 5 posts

Posted 17 June 2013 - 02:11 PM

public function get_paginacion_noticias($categoria)
{
$query="select * from noticias where id_categoria=$categoria order by id_noticia desc limit 1,5";
$result=mysqli_query(Conexion::conecta(),$query);

while ($reg=mysqli_fetch_assoc($result)) {
$this->noticia[]=$reg;
}
return $this->noticia;
}

quisiera poder decirle a mysql que esa variable $categoria sean todos los valores de esa columna, que valor le puedo dar a esa variable para que mysql me devuelvas todos los valores y no solo el de una fila?


  • 0

#2 poliburro

poliburro

    Advanced Member

  • Administrador
  • 4945 posts
  • LocationMéxico

Posted 17 June 2013 - 02:15 PM

Yo te recomendaría que agregaras una sentencia de selección de tal manera que si $categoría está vacio no incluya el where.
Por ejemplo:


public function get_paginacion_noticias($categoria) {

  $query="select * from noticias";
  if ($categoria)
    $query=$query." where id_categoria=$categoria ";
  $query  = $query." order by id_noticia desc limit 1,5";
  $result=mysqli_query(Conexion::conecta(),$query);
  while ($reg=mysqli_fetch_assoc($result)) {
      $this->noticia[]=$reg;
  }
  return $this->noticia;
}

  • 0

#3 iosujamed

iosujamed

    Newbie

  • Miembros
  • Pip
  • 5 posts

Posted 17 June 2013 - 03:00 PM

muchas gracias poliburro tu recomendacion me hizo repensar un poco y la adapte asi para el contexto con el que trabajo:
public function get_paginacion_noticias($inicio,$categoria)
{
if ($categoria!=0) {
$query="select * from noticias where id_categoria=$categoria order by id_noticia desc limit $inicio,5";
}else{
$query="select * from noticias order by id_noticia desc limit $inicio,5";
}

$result=mysqli_query(Conexion::conecta(),$query);

while ($reg=mysqli_fetch_assoc($result)) {
$this->noticia[]=$reg;
}
return $this->noticia;
}



me funciona excelente gracias (y)

  • 0




IP.Board spam blocked by CleanTalk.