Can´t see the bug in this short code

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Can´t see the bug in this short code

Post 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);
}
?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

Yeah, it works, but now I have the same result repeated millions of times.
:cry: and everything seems to be right.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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!
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post 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)
}
?>
Post Reply