Jump to content


Photo

SELECT from AND AND AND


  • Please log in to reply
7 replies to this topic

#1 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 August 2006 - 04:16 PM

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!

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 15 August 2006 - 04:24 PM

Shouldnt be any problems with adding multiple ANDs in a query statement.

Though you are making the result more exact when you keep adding ANDs.  How many entries is it returning to you?

How many items to you display on each page?

#3 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 August 2006 - 04:34 PM

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; // Limit the prints to 4

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!

#4 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 August 2006 - 05:38 PM

help please, thanks

#5 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 August 2006 - 07:57 PM

:

help please, thanks

:)

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 August 2006 - 08:06 PM

have you forgotten to pass $precio after the page is reloaded??
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 960 posts

Posted 15 August 2006 - 08:26 PM

A good idea is to save the sql in a string first. Then use the string. If there is a problem then you could coment out the execution and insert an echo statment. You can now look at the statment in detail to see were things are wrong. good idea to use a sting firs.


Des.


#8 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 17 August 2006 - 11:33 AM

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!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users