PHP PDOStatement::bindValue 教程
最后修改于 2025 年 4 月 19 日
PDOStatement::bindValue 方法将一个值绑定到预处理语句中的参数。它对于 PHP 中安全的数据库操作至关重要。
基本定义
PDOStatement::bindValue 将一个值绑定到 SQL 语句中的参数。与 bindParam 不同,它在调用 bindValue 时绑定该值。
语法:PDOStatement::bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR)
。param 可以是命名的(:name)或位置的(?)。type 指定数据类型。
基本的 bindValue 用法
这展示了将 bindValue 与预处理语句结合使用的最简单方法。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id'); $stmt->bindValue(':id', 5, PDO::PARAM_INT); $stmt->execute(); $user = $stmt->fetch(); if ($user) { echo "User found: {$user['name']}"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
这会将一个整数值绑定到一个命名参数。第三个参数将数据类型指定为整数。在调用 bindValue 时绑定该值。
绑定多个值
演示将多个值绑定到不同的参数类型。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('INSERT INTO products (name, price, active) VALUES (?, ?, ?)'); $stmt->bindValue(1, 'Laptop', PDO::PARAM_STR); $stmt->bindValue(2, 999.99, PDO::PARAM_STR); $stmt->bindValue(3, true, PDO::PARAM_BOOL); $stmt->execute(); echo "Product added successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
这会将三个不同的值类型绑定到位置参数。请注意,浮点数应使用 PARAM_STR。布尔值使用 PARAM_BOOL。
绑定 NULL 值
展示如何正确将 NULL 值绑定到数据库列。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('UPDATE users SET middle_name = :middle WHERE id = :id'); $stmt->bindValue(':middle', null, PDO::PARAM_NULL); $stmt->bindValue(':id', 10, PDO::PARAM_INT); $stmt->execute(); echo "Record updated with NULL value"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
要绑定 NULL 值,请使用 PDO::PARAM_NULL 作为类型参数。这可确保数据库收到正确的 NULL 值,而不是空字符串。
绑定大字符串
演示将大型文本值绑定到数据库列。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $largeText = file_get_contents('large_document.txt'); $stmt = $pdo->prepare('INSERT INTO documents (title, content) VALUES (:title, :content)'); $stmt->bindValue(':title', 'User Manual', PDO::PARAM_STR); $stmt->bindValue(':content', $largeText, PDO::PARAM_LOB); $stmt->execute(); echo "Large document stored successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
对于大型字符串或二进制数据,请使用 PDO::PARAM_LOB。这会将数据作为大型对象处理,这对于内存使用更有效。
绑定日期
展示如何正确将日期值绑定到数据库列。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $birthDate = new DateTime('1990-05-15'); $formattedDate = $birthDate->format('Y-m-d'); $stmt = $pdo->prepare('INSERT INTO employees (name, birth_date) VALUES (:name, :date)'); $stmt->bindValue(':name', 'Alice Johnson', PDO::PARAM_STR); $stmt->bindValue(':date', $formattedDate, PDO::PARAM_STR); $stmt->execute(); echo "Employee record created with date"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
日期应格式化为字符串,以数据库的预期格式。示例使用 Y-m-d 格式,这是 MySQL 日期的标准格式。
绑定数组
演示一种将数组绑定到 IN 子句的技术。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $ids = [3, 7, 12, 15]; $placeholders = rtrim(str_repeat('?,', count($ids)), ','); $stmt = $pdo->prepare("SELECT * FROM products WHERE id IN ($placeholders)"); foreach ($ids as $index => $id) { $stmt->bindValue($index + 1, $id, PDO::PARAM_INT); } $stmt->execute(); $products = $stmt->fetchAll(); echo "Found " . count($products) . " products"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
PDO 不直接支持数组绑定。这会动态创建占位符,并单独绑定每个数组元素。索引从 1 开始。
使用不同的数据类型绑定
展示将各种 PHP 数据类型绑定到相应的 SQL 类型。
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('INSERT INTO test_data (int_val, float_val, bool_val, str_val) VALUES (?, ?, ?, ?)'); $stmt->bindValue(1, 42, PDO::PARAM_INT); $stmt->bindValue(2, 3.14159, PDO::PARAM_STR); // Float as string $stmt->bindValue(3, false, PDO::PARAM_BOOL); $stmt->bindValue(4, 'Hello World', PDO::PARAM_STR); $stmt->execute(); echo "Data with different types inserted"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
这演示了将不同的 PHP 类型绑定到 SQL 参数。请注意,浮点数应作为字符串绑定,以避免精度问题。
最佳实践
- 始终指定数据类型:不要依赖自动类型检测。
- 使用命名参数:它们使代码更具可读性。
- 绑定前验证:确保数据符合预期格式。
- 重用语句:准备一次,绑定/执行多次。
- 关闭语句:完成时释放资源。
来源
本教程介绍了 PDOStatement::bindValue
方法,并提供了实际示例,展示了不同的绑定场景和数据类型。
作者
列出 所有 PHP PDO 函数。