Jump to content

SELECT from AND AND AND


masgas

Recommended Posts

Hi again!
I am paginating some mysql results, but I can't get it to work... If I write:

$resultados = mysql_query("SELECT * FROM articulos WHERE visible = 1 AND sexo LIKE '%$sexo%' LIMIT $inicio, $registros");

seems to work ok. BUT if I add another AND and the query is like this:

$resultados = mysql_query("SELECT * FROM articulos WHERE visible = 1 AND sexo LIKE '%$sexo%' AND precio <= '$precio' LIMIT $inicio, $registros");

then the pagination doesn't work, it just shows the pages available but when you click on them no results are printed...

Is there a problem with adding AND many times to a single query?

Thanks a lot!
Link to comment
https://forums.phpfreaks.com/topic/17640-select-from-and-and-and/
Share on other sites

it seems to select correctly because it gives me  a 1 out of 17 pages which is more or less the amount of pages there should be!
And it limits the prints to 4 which is the amount I have limited the page to do.

$prints = 4; // [color=green]Limit the prints to 4[/color]

if (!$page) {
  $start = 0;
  $page = 1;
}
else {
  $start = ($page - 1) * $prints;
}
$results = mysql_query("SELECT id FROM articulos WHERE sexo LIKE '%$sexo%' AND precio <= '$precio' and visible = 1");
$total_prints = mysql_num_rows($results);
$results = mysql_query("SELECT * FROM articulos WHERE sexo LIKE '%$sexo%' and visible = 1 AND precio <= '$precio' LIMIT $start, $prints");
$total_pages = ceil($total_prints / $prints);

while($articulo=mysql_fetch_array($results)) {

  echo "<b>".$articulo["direccion"]."</b><br>";
  echo "<b>".$articulo["user"]."</b><br>";
  echo "<b>".$articulo["m2"]."</b><br>";
  echo "<b>".$articulo["totab"]."</b><br>";
}

if(($page - 1) > 0) {
  echo "<a href='paginara.php?page=".($page-1)."'>Back</a> ";
}
for ($i=1; $i<=$total_pages; $i++){
  if ($page == $i)
      echo "<b>".$page."</b> ";
  else
      echo "<a href='paginara.php?page=$i'>$i</a> ";
}

if(($page + 1)<=$total_pages) {
  echo " <a href='paginara.php?page=".($page+1)."'>Next</a>";
}

Any guess where the error might be? thanks!
Excellent work! (yours of course!) cause I had forgotten to pass the variables over the pages so it looks like this now, and it works:


$registros = 4;

if (!$pagina) {
  $inicio = 0;
  $pagina = 1;
}
else {
  $inicio = ($pagina - 1) * $registros;
}
//$res = mysql_query ("SELECT * FROM articulos WHERE sexo LIKE '%$sexo%' AND precio <= '$precio' ORDER BY precio");
$resultados = mysql_query("SELECT * FROM articulos WHERE visible = '1' AND sexo LIKE '%$sexo%' AND precio <= '$precio'");
$total_registros = mysql_num_rows($resultados);
$resultados = mysql_query("SELECT * FROM articulos WHERE visible != '' AND sexo LIKE '%$sexo%' AND precio <= '$precio' LIMIT $inicio, $registros");
$total_paginas = ceil($total_registros / $registros);
//sexo LIKE '%$sexo%' AND precio <= '$precio'"
//ORDER BY precio
//id, direccion, user, imagen, m2, habitaciones, totab, otros, email, telefono, precio, alquilar
if($total_registros) {
while($articulo=mysql_fetch_array($resultados)) {

  echo "<b>".$articulo["direccion"]."</b><br>";
  echo "<b>".$articulo["user"]."</b><br>";
  echo "<b>".$articulo["m2"]."</b><br>";
  echo "<b>".$articulo["totab"]."</b><br>";
}
} else {
echo "<font color='darkgray'>(sin resultados)</font>";
}

mysql_free_result($resultados);

if($total_registros) {
if(($pagina - 1) > 0) {
  echo "<a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=".($pagina-1)."'>Anterior</a> ";
}
for ($i=1; $i<=$total_paginas; $i++){
  if ($pagina == $i)
      echo "<b>".$pagina."</b> ";
  else
      echo "<a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=$i'>$i</a> ";
}

if(($pagina + 1)<=$total_paginas) {
  echo " <a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=".($pagina+1)."'>Siguiente</a>";
}
}


thanks a lot!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.