PDO mysql LIMIT with placeholder in an array

BernardA

This is one more on the LIMIT clause and PDO, but I could not find a solution elsewhere.

I'm getting a PDO error and I know perfectly well why.

PDO requires an integer on LIMIT clause, but as the variables are included as part of the given parameter array, PDO treats all of it as string, hence the error.

The question is: how can I set this up to avoid the error?

For general information, this is part of a " load more " set up.

SQLSTATE[42000]: Syntax error or access violation

This is the query:

$param=array();
$param = array_merge($param,$geocities);
$param[] = $limit;
$param[] = $offset;
$in  = str_repeat('?,', count($geocities) - 1) . '?';
    $sql = "SELECT ads.*, cities.city  FROM ads
            INNER JOIN cities USING(city_id)
            WHERE cities.city_id IN ($in)
            AND ads.published IS NOT NULL
            AND ads.deleted IS NULL
            ORDER BY ad_id
            LIMIT ?
            OFFSET ?";
    $stmt = $ulink->prepare($sql);
    $stmt->execute($param);
Don't Panic

Instead of calling execute() with an array argument, you should be able to loop over $param and bind the values as INT one at a time before executing.

$stmt = $ulink->prepare($sql);
$i = 1;
foreach ($param as $value) {
    $stmt->bindValue($i++, $value, PDO::PARAM_INT);
}
$stmt->execute();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL returning PDO placeholder names

From Dev

MySQL PDO limit issue

From Dev

MySQL PDO LIMIT

From Dev

INSERT INTO array with MySQL and PDO

From Dev

array for PDO MySQL function

From Dev

PDO MySQL LIMIT query unexpected result

From Dev

Received "SQLSTATE[42000]: LIMIT ERROR" when using MySQL via PDO

From Dev

PHP PDO error when using placeholders in the LIMIT clause of a MySQL query

From Dev

PDO equivalent of mysql_fetch_array

From Dev

Escape Array of Strings for IN Statement PDO MYSQL

From Dev

Error fetching MySQL array using PDO

From Dev

PDO: Inserting array in MySQL with Incremental Keys

From Dev

JS array to PHP and update table in MYSQL with PDO

From Dev

Same array multiple times in MySQL "IN" query with PDO

From Dev

Create array from MySQL PDO query results

From Dev

PDO: Inserting array in MySQL with Incremental Keys

From Dev

mySQL responses "array" at PHP page (PDO)

From Dev

PDO UPDATE array using php mysql

From Dev

JS array to PHP and update table in MYSQL with PDO

From Dev

PDO Prepared Statement with % and Limit

From Dev

Change date format for array to MySQL format using PDO

From Dev

Selecting MySql table data into an array using PDO class?

From Dev

translation mysql_fetch_array to PDO::FETCH_NUM

From Dev

PHP MySQL PDO selecting rows on array with id's

From Dev

translation mysql_fetch_array to PDO::FETCH_NUM

From Dev

PHP PDO MYSQL - Get Array comma separated from fetchAll

From Dev

PHP PDO: Search array value inside of MySQL's JSON object

From Dev

PHP PDO SELECT LIMIT issue

From Dev

Variable limit on dataset using PDO