note 128138 added to pdo.prepare

From: Date: Sat, 21 Jan 2023 13:42:02 +0000
Subject: note 128138 added to pdo.prepare
Groups: php.notes 
Request: Send a blank email to php-notes+get-220093@lists.php.net to get a copy of this message
There are restrictions on the placeholder string. In the following code the first execute fails with a SQLSTATE[HY093]. It is not clear exactly what characters are allowed. <?php declare(strict_types=1); $db = new \PDO("mysql:hostname=localhost;dbname=minidwh", "minidwh", "Meisterstueck!"); $db->query("SET NAMES 'utf8mb4'"); $db->query("DROP TABLE IF EXISTS äëïöüß"); $db->query("CREATE TABLE äëïöüß ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ) ENGINE = ARIA;"); $db->query("ALTER TABLE äëïöüß ADD COLUMN äëïöüß TEXT NULL"); try { $stmt = $db->prepare("INSERT INTO äëïöüß (äëïöüß) VALUES (:äëïöüß)"); $result = $stmt->execute([':äëïöüß' => 'test1']); } catch (\PDOException $e) { echo $e->getMessage() . '<BR>'; } try { $stmt = $db->prepare("INSERT INTO äëïöüß (äëïöüß) VALUES (?)"); $result = $stmt->execute(['test2']); } catch (\PDOException $e) { echo $e->getMessage() . '<BR>'; } try { $stmt = $db->prepare("INSERT INTO äëïöüß (äëïöüß) VALUES (:column)"); $result = $stmt->execute([':column' => 'test3']); } catch (\PDOException $e) { echo $e->getMessage() . '<BR>'; } try { $stmt = $db->prepare("INSERT INTO äëïöüß (äëïöüß) VALUES (:column)"); $stmt->bindValue(':column', 'test4'); $result = $stmt->execute(); } catch (\PDOException $e) { echo $e->getMessage() . '<BR>'; } ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.158.213 (proxied: 83.97.81.249) ---- Manual Page -- https://php.net/manual/en/pdo.prepare.php Edit -- https://main.php.net/note/edit/128138 Del: integrated -- https://main.php.net/note/delete/128138/integrated Del: useless -- https://main.php.net/note/delete/128138/useless Del: bad code -- https://main.php.net/note/delete/128138/bad+code Del: spam -- https://main.php.net/note/delete/128138/spam Del: non-english -- https://main.php.net/note/delete/128138/non-english Del: in docs -- https://main.php.net/note/delete/128138/in+docs Del: other reasons-- https://main.php.net/note/delete/128138 Reject -- https://main.php.net/note/reject/128138 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220093) next »