-1
I have an SQL query in PHP to get the information according to the line number of a file entered by the user. I need to take this number of rows of a variable and replace it with the variable in SQL.
Currently I have this code in the function:
$conn = $this->connect();
$sql = " select * from requisicao r
inner join usuario u on r.id_requisitante = u.id_usuario
left join aprovador a on a.id_aprovador = r.id_aprovador
left join categoria_requisicao cr on cr.id_cat_requisicao = r.id_cat_requisicao
left join categoria c on c.id_categoria = cr.id_categoria
left join sub_categoria_requisicao scr on scr.id_sub_cat_req = r.id_sub_cat_req
left join sub_categoria sc on sc.id_sub_categoria = scr.id_sub_categoria
left join fornecedor_requisicao fr on fr.id_fornecedor_requisicao = r.id_fornecedor_requisicao
left join fornecedor f on f.id_fornecedor = fr.id_fornecedor
where r.id_requisitante = :id_usuario ORDER BY id_requisicao DESC limit :numeroL ";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id_usuario', $id_usuario);
$stmt->bindParam(':numeroL', $numeroL);
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
But it makes a mistake:
Fatal error : Uncaught Pdoexception: SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax;
have you tried to display how the query looked in "$sql" and see if there are any errors, or still take the query and try to run directly in the database? By the message the query was badly formed, it may be time to replace the parameters. Where the variable comes from
$numeroL
? ask in your question– Ricardo Pontual
The query works in sql when I do not put the variable and put the number itself, when I try to use mysql itself to pass a variable of the same error. The variable comes from another function I tested display it and ta picking the right value.
– Vanessa Lemos
Check whether the parameters
:id_usuario
and:numeroL
are with values. I noticed that inORDER BY id_requisicao
the fieldid_requisicao
does not appear in the rest of the query, should not beORDER BY r.id_requisitante
?– Clarck Maciel