PHP8 cubrid_bind
(PECL CUBRID >= 8.3.0)
cubrid_bind — 将变量作为参数绑定到预准备语句
说明
cubrid_bind(
resource $req_identifier,
int $bind_index,
mixed $bind_value,
string $bind_value_type = ?
): bool
cubrid_bind() 函数用于将值绑定到 SQL 语句中对应的命名或问号占位符 已传递给 cubrid_prepare()。如果未给出,则字符串将是 违约。bind_value_type
注意:如果要绑定的数据类型是 BLOB/CLOB,CUBRID 将尝试映射 数据作为 PHP 流。如果实际绑定值类型不是 stream, CUBRID会将其转换为字符串,并将其用作完整路径和文件 客户机文件系统上的文件名。如果要显式绑定的数据类型是 ENUM,则参数应为字符串格式的 enum 元素。bind_value在 CUBRID 分片环境中,必须包含在 cubrid_bind() 函数。bind_value_type
下表显示了替换值的类型。
Support | Bind Type | Corresponding SQL Type |
---|---|---|
Supported | STRING | CHAR, VARCHAR |
NCHAR | NCHAR, NVARCHAR | |
BIT | BIT, VARBIT | |
NUMERIC or NUMBER | SHORT, INT, NUMERIC | |
FLOAT | FLOAT | |
DOUBLE | DOUBLE | |
TIME | TIME | |
DATE | DATE | |
TIMESTAMP | TIMESTAMP | |
OBJECT | OBJECT | |
ENUM | ENUM | |
BLOB | BLOB | |
CLOB | CLOB | |
NULL | NULL | |
Not supported | SET | SET |
MULTISET | MULTISET | |
SEQUENCE | SEQUENCE |
参数 ¶
req_identifier
请求标识符作为 cubrid_prepare() 的结果。
bind_index
绑定参数的位置。它以 1 开头。
bind_value
绑定的实际值。
bind_value_type
要绑定的值的类型。(默认情况下省略。 因此,默认情况下,系统内部使用字符串。但是,您需要 当值为 NCHAR 时,将值的确切类型指定为参数, BIT 或 BLOB/CLOB)。
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
版本 | 说明 |
---|---|
8.3.1 | 添加了 BLOB/CLOB 数据类型支持。 |
示例
示例 #1 cubrid_bind() example
<?php
$conn = cubrid_connect("localhost", 33000, "demodb", "dba");
$result = cubrid_execute($conn, "SELECT code FROM event WHERE sports='Basketball' and gender='M'");
$row = cubrid_fetch_array($result, CUBRID_ASSOC);
$event_code = $row["code"];
cubrid_close_request($result);
$game_req = cubrid_prepare($conn, "SELECT athlete_code FROM game WHERE host_year=1992 and event_code=? and nation_code='USA'");
cubrid_bind($game_req, 1, $event_code, "number");
cubrid_execute($game_req);
printf("--- Dream Team (1992 United States men's Olympic basketball team) ---\n");
while ($athlete_code = cubrid_fetch_array($game_req, CUBRID_NUM)) {
$athlete_req = cubrid_prepare($conn, "SELECT name FROM athlete WHERE code=? AND nation_code='USA' AND event='Basketball' AND gender='M'");
cubrid_bind($athlete_req, 1, $athlete_code[0], "number");
cubrid_execute($athlete_req);
$row = cubrid_fetch_assoc($athlete_req);
printf("%s\n", $row["name"]);
}
cubrid_close_request($game_req);
cubrid_close_request($athlete_req);
cubrid_disconnect($conn);
?>
以上示例会输出:
--- Dream Team (1992 United States men's Olympic basketball team) --- Stockton John Robinson David Pippen Scottie Mullin C. Malone Karl Laettner C. Jordan Michael Johnson Earvin Ewing Patrick Drexler Clyde Bird Larry Barkley Charles
示例 #2 cubrid_bind() BLOB/CLOB example
<?php
$con = cubrid_connect("localhost", 33000, "demodb", "dba", "");
if ($con) {
cubrid_execute($con,"DROP TABLE if exists php_cubrid_lob_test");
cubrid_execute($con,"CREATE TABLE php_cubrid_lob_test (doc_content CLOB)");
$sql = "INSERT INTO php_cubrid_lob_test(doc_content) VALUES(?)";
$req = cubrid_prepare($con, $sql);
$fp = fopen("book.txt", "rb");
cubrid_bind($req, 1, $fp, "clob");
cubrid_execute($req);
}
?>
示例 #3 cubrid_bind() BLOB/CLOB 示例
<?php
$con = cubrid_connect("localhost", 33000, "demodb", "dba", "");
if ($con) {
cubrid_execute($con,"DROP TABLE if exists php_cubrid_lob_test");
cubrid_execute($con,"CREATE TABLE php_cubrid_lob_test (image BLOB)");
$sql = "INSERT INTO php_cubrid_lob_test(image) VALUES(?)";
$req = cubrid_prepare($con, $sql);
cubrid_bind($req, 1, "cubrid_logo.png", "blob");
cubrid_execute($req);
}
?>
参见
- cubrid_execute() - 执行准备好的 SQL 语句
- cubrid_prepare() - 准备要执行的 SQL 语句