PHP PDO & # 8594; Prepared request return error (the same request is not prepared, working fine)

I use the following sql to get the value of the que_id field of a specific row of my table, and it works fine. Note that que_id (auto-incremented) and line numbers do not match.

 $qry_que_getid = $connexion->query('SELECT somefield FROM table ORDER BY somefield ASC LIMIT '.$lineNumberSeeked.', 1'); $row = $qry_que_getid->fetch(PDO::FETCH_ASSOC); echo $row['que_id']; 

When I try to convert this query into a prepared query as follows, I have an error and I don’t understand:

 $qry_que_getid = $connexion->prepare('SELECT somefield FROM table ORDER BY somefield ASC LIMIT ?, 1'); $qry_que_getid->execute(array(4)); $row = $qry_que_getid->fetch(PDO::FETCH_ASSOC); echo $row['que_id']; 

I get the following 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 ''3', 1' at line 1 error 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 ''3', 1' at line 1 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 ''3', 1' at line 1

Hope somene can help me understand. Thanks in advance. Greetings. Mark.

+7
source share
1 answer

From the PHP manual at http://www.php.net/manual/en/pdostatement.execute.php :

An array of values ​​with as many elements as the SQL statement being executed has associated parameters. All values ​​are processed as PDO :: PARAM_STR.

The LIMIT clause expects an integer, which I count, so you should use the bindParam () method instead.

 $limit = 4; $qry_que_getid->bindParam(1, $limit, PDO::PARAM_INT); $qry_que_getid->execute(); 

Otherwise, the parameter will be passed as type PDO :: PARAM_STR instead of the expected PDO :: PARAM_INT.

+4
source

All Articles