Home > Backend Development > PHP Tutorial > What Characters Are Valid for PDO Placeholders in PHP?

What Characters Are Valid for PDO Placeholders in PHP?

DDD
Release: 2024-12-06 08:18:16
Original
823 people have browsed it

What Characters Are Valid for PDO Placeholders in PHP?

Valid PDO Placeholder Characters

Working with placeholders in PHP with PDO can be tricky when it comes to selecting valid characters. To delve into the details, let's first examine the example code provided:

$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
Copy after login

In this example, we're using the placeholder ":colour." What characters are considered valid for placeholders? The answer lies in the source code. Upon inspection, we discover the following regular expression:

BINDCHR     = [:][a-zA-Z0-9_]+;
Copy after login

This means that a placeholder must consist of the following elements:

  • An optional leading colon (:)
  • At least one alphanumeric character (a-z, A-Z, 0-9)
  • An optional trailing underscore (_)

Therefore, valid placeholder characters include:

  • All lowercase letters (a-z)
  • All uppercase letters (A-Z)
  • All numbers (0-9)
  • The underscore character (_)

Hyphens (-), as mentioned in the question, are not valid characters. Alphanumeric characters with underscores provide a convenient and comprehensive range of options for placeholder names.

The above is the detailed content of What Characters Are Valid for PDO Placeholders in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template