Hongmu Notes
Home Summary of pitfalls Kakaka, one bug can be fixed in one day! <b>Fatal error</b>: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
Summary of pitfalls

Kakaka, one bug can be fixed in one day! <b>Fatal error</b>: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in

Kakaka, one bug can be fixed in one day! <b>Fatal error</b>: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in

about$stmt->execute($categoryIds); – Issue with parameter passing.

I'm really done with this—just one little problem and it's kept me up all afternoon!
It wasn't until I checked the documentation in the evening that I realized the problem was actually with this thing.

PDOException: SQLSTATE[HY093]  Error; The root cause is ; Parameter placeholders in SQL statements; execute() The imported array parameter cannot be correctly matched.

Error Cause Diagnosis

execute() 期望输入一个值Indexed array(in compliance with  ['value1', 'value2'])perhapsAssociative array(in compliance with  [':name' => 'value']), used to map one-to-one to the corresponding entries in SQL; ?   or  ; :name Placeholder

if  $categoryIds If the requirements are not met, an error will be reported. Common issues include:

  • Array key names are not consecutive.If; $categoryIds  was processed; array_filter() When functions are applied to the data, it may result in a "sparse array" (for example, where the key names are 0, 2, 3, but the value for 1 is missing).execute() Unable to correctly handle this discontinuous index.

  • Invalid key nameIf you are using named placeholders in SQL (e.g.,  ); :id), then ; $categoryIds The key name must be exactly the same (e.g.,  ); [':id' => 1]). Incorrect key name or use of invalid characters (e.g., period ); .) will cause a matching failure

  • Number of parameters does not match$categoryIds The number of parameters must exactly match the number of placeholders in the SQL statement.

  • multidimensional arrayIf; $categoryIds A multidimensional array (e.g., a result set retrieved from a database),execute() 也无法处理

solution

1. Check and reset array indices (most common cause)

If you have used it; array_filter() Or a similar function; please use   first. array_values() Reset Index:

// 假设 $categoryIds 来自过滤
// $categoryIds = array_filter($categoryIds);
$categoryIds = array_values($categoryIds); // 重置索引为 0, 1, 2...
$stmt->execute($categoryIds);

2. Ensure that the key name matches the placeholder.

If you are using named placeholders (e.g.,  ); :cat_id),please check it:

// 正确的做法
$sql = "SELECT * FROM articles WHERE category_id IN (:cat_id1, :cat_id2)";
$stmt = $pdo->prepare($sql);
// $categoryIds 的键名必须和占位符完全一致
$stmt->execute([':cat_id1' => 1, ':cat_id2' => 2]);

3. Use; ?  Placeholder (simpler)

if  $categoryIds It is a simple index array (e.g., [1, 2, 3]), Use; ? Placeholders will be more convenient:

// 生成对应数量的 ? 占位符
$placeholders = rtrim(str_repeat('?,', count($categoryIds)), ',');
$sql = "SELECT * FROM articles WHERE category_id IN ($placeholders)";
$stmt = $pdo->prepare($sql);
$stmt->execute($categoryIds); // 直接传入索引数组

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Browser caching increases the speed of secondary visits to your website

Browser caching increases the speed of secondary visits to your website Summary of pitfalls

Use browser caching of official words: If users will visit your website multiple times, browser caching of static resources can save users time. Cache headers should be applied to all cacheable static resources, not just to a small subset of static resources (for example, images). Cacheable resources include JS and CSS files, image files, and other binary object files (media files, PDF files, etc.). Normally, HTML doesn’t…
👁 202

Recommended reading

php split a string into its constituent characters

php split a string into its constituent characters Language Notes PHP

Question: How to split a string into its constituent characters in PHP? For example, hello -> [h, e, l, l, o] There are three methods: This is the string that needs to be split: $str = 'Hello sample'; The length of the string: $len = mb_strlen($str, 'utf8'); // 7 The first way: $arr = str_…
👁 182
(PC+WAP) Blue Hardware and Machinery Website Template – pbootCMS Enterprise Website Template; Download General Marketing Website Source Code – 0106

(PC+WAP) Blue Hardware and Machinery Website Template – pbootCMS Enterprise Website Template; Download General Marketing Website Source Code – 0106 Practical Collection pbootcms Template

A versatile blue hardware and machinery PbootCMS corporate website template designed for various machinery manufacturing and hardware processing industries, compatible with both PC and mobile devices. It features a clean, professional design and comprehensive functional modules. This template is an ideal choice for machinery and equipment companies looking to quickly build a marketing-oriented branded official website and enhance their online promotion effectiveness. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extract password...
👁 41
(Adaptive mobile version) PBootCMS template for film and television news websites; Download source code for film and television news blog websites – 0346

(Adaptive mobile version) PBootCMS template for film and television news websites; Download source code for film and television news blog websites – 0346 Practical Collection pbootcms Template

An adaptive mobile-friendly PbootCMS website template for film and television content and news blogs. Featuring an entertaining and trendy design, this template is ideal for sharing movie reviews, film and television news, and industry updates. It enables film critics or movie and TV enthusiasts to create an online content-sharing platform. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 59
(Adaptive mobile version) Responsive primary and secondary school essay writing website – PBootCMS template; Download source code for article, information, academic paper, and personal blog websites – 0585

(Adaptive mobile version) Responsive primary and secondary school essay writing website – PBootCMS template; Download source code for article, information, academic paper, and personal blog websites – 0585 Practical Collection pbootcms Template

A responsive PbootCMS website template designed for primary and secondary school students and educators to create personal blogs or publish essays and academic papers; compatible with both PC and WAP devices. Its fresh, artistic design makes it ideal for students, teachers, or literature enthusiasts to share their essays, prose, and educational insights. This template helps you build a wholesome and uplifting literary sharing platform. Template Demo | Installation Instructions | Website Admin Panel: /admin.php | Username: admin | Password: admin | Extraction Password: w...
👁 56