Error in query: 'SQLSTATE [42000]

-1

I am doing data call of the database with php and PDO, but whenever I do it with bindparam(); it gives me the following error:

  

Fatal error : Uncaught exception 'PDOException' with message 'SQLSTATE [42000]: Syntax error 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 '' SELECT * FROM   data 'LIMIT 0,' 5 '' at line 1 'in   D: \ xampp \ htdocs \ pagination \ index.php: 25 Stack trace: # 0   D: \ xampp \ htdocs \ paginacao \ index.php (25): PDOStatement-> execute () # 1   {main} thrown in D: \ xampp \ htdocs \ page \ index.php on line 25

Here's the code below to help:

<?php
require_once "class.user.php";

$liga = new USER();
$busca = "SELECT * FROM dados";

// número de registros por página

$total_reg = "5";
$pagina = $_GET['pagina'];

if (!$pagina)
    {
    $pc = "1";
    }
  else
    {
    $pc = $pagina;
    }

$inicio = $pc - 1;
$inicio = $inicio * $total_reg;
$limite = $res = $liga->runQuery(':busca LIMIT :ini,:totalreg');
$res->bindparam(':busca', $busca, PDO::PARAM_STR);
$res->bindparam(':ini', $inicio, PDO::PARAM_INT);
$res->bindparam(':totalreg', $total_reg, PDO::PARAM_STR);
$res->execute();
$todos = $res1 = $liga->runQuery(":busca");
$res1->bindparam(":busca", $busca);
$res1->execute();
$tr = $res1->rowCount($todos);

// verifica o número total de registros

$tp = $tr / $total_reg;

// verifica o número total de páginas // vamos criar a visualização

while ($dados = $limite->fetch(PDO::FETCH_ASSOC))
    {
    $nome = $dados["nome"];
    echo "Nome: $nome<br />";
    } // agora vamos criar os botões "Anterior e próximo"
$anterior = $pc - 1;
$proximo = $pc + 1;

if ($pc > 1)
    {
    echo " <a href='?pagina=$anterior'><- Anterior</a> ";
    }

echo "|";

if ($pc < $tp)
    {
    echo " <a href='?pagina=$proximo'>Próxima -></a>";
    }

?>

Where is the problem that causes this error?

    
asked by anonymous 19.04.2016 / 12:50

1 answer

0

The problem is that you are concatenating the first query as a string parameter, that is, you are saying that the

SELECT * FROM dados

It is actually a string, not a query, to solve you can add the whole code in the first variable (since you perform the filter by limit every time):

$busca = "SELECT * FROM dados LIMIT :ini,:totalreg";
$limite = $res = $liga->runQuery($busca);
$res->bindparam(':ini', $inicio, PDO::PARAM_INT);
$res->bindparam(':totalreg', $total_reg, PDO::PARAM_INT);
$res->execute();
    
19.04.2016 / 12:58