Error executing query in php

0

Good, I am trying to separate a query from a web page in different pages but at the time of making the query I get the following error:

  

Fatal error: Uncaught PDOException: SQLSTATE [42000]: Syntax error or or   access violation: 1064 You have an error in your SQL syntax; check the   manual that corresponds to your MySQL server version for the right   syntax to use near '' 0 ',' 20 '' at line 1 in   C: \ wamp \ www \ php \ tfg \ web \ principal \ catalog.php: 58 Stack trace: # 0   C: \ wamp \ www \ php \ tfg \ web \ principal \ catalog.php (58):   PDOStatement-> execute () # 1 {main} thrown in   C: \ wamp \ www \ php \ tfg \ web \ main \ catalog.php on line 58

The code I am using is the following:

if (isset($_GET['categoria']) OR isset($_POST['categoria'])){
        if (isset($_GET["pagina"]) OR isset($_POST['pagina'])) { $pagina  = $_REQUEST["pagina"]; } else { $pagina=1; };
        $resultadoPorPagina=20;
        $empezar=($pagina-1) * $resultadoPorPagina;
        $cat=$_REQUEST['categoria'];
        $conexion= new PDO("mysql:host=$con;dbname=$bdname",$superUser,$passdb);
        $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $consulta=$conexion->prepare("SELECT * FROM productos where product_cat=:product_cat order by produc_id desc LIMIT :empezar,:resultadoPorPagina");
        $consulta->bindParam(":product_cat", $cat);
        $consulta->bindParam(":empezar", $empezar);
        $consulta->bindParam(":resultadoPorPagina", $resultadoPorPagina);
        $consulta->execute();
        $resultado=$consulta->fetchAll();
            foreach ($resultado as $fila) {
                $contador=0;
        ?> 
            <div class="cuerpo col-4 col-m-4">
                <img class='imgTabla' src="<?php echo $fila['produc_imgPrinc'];?>"/><br/>
                <a href="producto.php?idProducto=<?php echo $fila['produc_id'];?>"><?php $fila["produc_nombre"];?></a><br/>
                <div class="precio"><?php echo $fila['produc_precio'];?></div>         <form method="post" action="catalogo.php">
                <input type="hidden" name="idProduct" value="<?php echo $fila['produc_id'];?>"/>
                <input type="hidden" name="pagina" value="<?php echo $pagina;?>"/>
                <input type="hidden" name="categoria" value="<?php echo $cat;?>"/>
                <input type="button" name="addCarrito" value="Añadir al carro"/>
                </form>
            </div>
        <?php
        $contador++;
        if ($contador=3){
            echo "<br/>";
            $contador=1;
            }
    }
    
asked by emilio789 07.12.2017 в 00:03
source

1 answer

0

You are taking :empezar and :resultadoPorPagina as String and not as int, try replacing:

$consulta->bindParam(":empezar", $empezar);
$consulta->bindParam(":resultadoPorPagina", $resultadoPorPagina);

By these lines:

$consulta->bindValue(':empezar', (int) $empezar, PDO::PARAM_INT); 
$consulta->bindValue(':resultadoPorPagina', (int) $resultadoPorPagina, PDO::PARAM_INT); 
    
answered by 07.12.2017 / 00:09
source