Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use period dot in marked parameters for PDO prepared statements?

Tags:

php

pdo

For an SQL query involving multiple tables, how do I construct such PDO statement like this?

Because this doesn't work:

$stmt = $pdo -> prepare("UPDATE category, product 
    SET product.category_id = category.id, 
    product.xxx = :product.xxx, 
    category.yyy = :category.yyy 

    WHERE product.category_slug = category.slug 
    AND product.aaa = :product.aaa"
);

$stmt->execute(array(
  'product.xxx' => '',
  'category.yyy' => '',
  'product.aaa' => ''
));

Which gives these errors:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: 
Invalid parameter number: parameter was not defined'

PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

How do I make this work? PDO doesn't seem to allow period dots in marked parameters? I guess I'm doomed with underscores?

like image 779
datasn.io Avatar asked Feb 01 '26 15:02

datasn.io


1 Answers

Here are the allowed characters for named placeholders:

[:][a-zA-Z0-9_]+;

Alphanumeric and underscores.

Ref. https://github.com/php/php-src/blob/master/ext/pdo/pdo_sql_parser.re (this is the source)

like image 87
Kevin Avatar answered Feb 03 '26 04:02

Kevin