Page 1 of 1

Can´t see the bug in this short code

Posted: Fri Dec 17, 2004 4:16 am
by Perfidus
I'm getting this message from server but I can not see what's wrong:
FROM stock WHERE linea='macumba' AND almacen='Calle de Robocop' :You have an error in your SQL syntax near 'FROM stock WHERE linea='macumba' AND almacen='Calle de Robocop'' at line 1

Code: Select all

<?php
if (isset($linea) && $linea != -1)
   $clauses[] = "linea='".$linea."'"; 
if (isset($almacen) && $almacen != -1) 
   $clauses[] = "almacen='".$almacen."'";
$query1 = 'FROM stock WHERE';
if (count($clauses) > 0)
$query1 .= ' '.join(' AND ', $clauses);
$pregunta1=mysql_query('SELECT count(*) '.$query1, $conexion) or die($query1.' :'.mysql_error()); 
$coincidencias = array_shift(mysql_fetch_row($pregunta1));
$herencia = (!empty($_GET['herencia']) && is_numeric($_GET['herencia'])) ? $_GET['herencia'] : 0; 
$incremento = (!empty($_GET['incremento'])) ? $_GET['incremento'] : 0;
$aumento = $herencia + $incremento;
if ($aumento < 0) 
   $aumento = 0; 
$pregunta2 = 'SELECT * ' . $query1 . ' LIMIT ' . $aumento . ', 10'; 
$result2=mysql_query($query1, $conexion) or die($query1.' :'.mysql_error()); 
$regporpagina=mysql_num_rows($pegar);
}
?>

Posted: Fri Dec 17, 2004 4:35 am
by kettle_drum

Code: Select all

$pregunta2 = 'SELECT * ' . $query1 . ' LIMIT ' . $aumento . ', 10'; 
$result2=mysql_query($query1, $conexion) or die($query1.' :'.mysql_error());
Change $query1 to $pregunta2.

Posted: Fri Dec 17, 2004 5:02 am
by Perfidus
Yeah, it works, but now I have the same result repeated millions of times.
:cry: and everything seems to be right.

Posted: Fri Dec 17, 2004 5:08 am
by kettle_drum
Well echo out $pregunta2 to see exactly what query you are running. Also check the data in the database using something like phpMyAdmin - you might have lots of the same data in there!

Posted: Fri Dec 17, 2004 5:29 am
by Perfidus
I have already echoed $pregunta2 and I have checked out with MyAdmin what's inside DB and everything is ok.
I'm echoing results like this:

Code: Select all

<?php
while($row2 = mysql_fetch_array($result2)) { 
   foreach($row2 as $column)
}
?>