(PECL CUBRID >= 8.3.0)
cubrid_bind — 将变量绑定到预处理语句作为参数
cubrid_bind() 函数用于将值绑定到传递给 cubrid_prepare() 的 SQL 语句中相应的命名或问号占位符。如果未指定 bind_value_type
,则默认类型为字符串。
注意:
如果要绑定的数据类型为 BLOB/CLOB,CUBRID 将尝试将数据映射为 PHP 流。如果实际绑定值类型不是流,CUBRID 会将其转换为字符串,并将其用作客户端文件系统上的文件完整路径和文件名。
如果要绑定的数据类型显式指定为 ENUM,则
bind_value
参数应为字符串格式的枚举元素。在 CUBRID 分片环境中,必须在 cubrid_bind() 函数中包含
bind_value_type
。
下表显示了替代值的类型。
支持 | 绑定类型 | 对应 SQL 类型 |
---|---|---|
支持 | STRING | CHAR,VARCHAR |
NCHAR | NCHAR,NVARCHAR | |
BIT | BIT,VARBIT | |
NUMERIC 或 NUMBER | SHORT,INT,NUMERIC | |
FLOAT | FLOAT | |
DOUBLE | DOUBLE | |
TIME | TIME | |
DATE | DATE | |
TIMESTAMP | TIMESTAMP | |
OBJECT | OBJECT | |
ENUM | ENUM | |
BLOB | BLOB | |
CLOB | CLOB | |
NULL | NULL | |
不支持 | SET | SET |
MULTISET | MULTISET | |
SEQUENCE | SEQUENCE |
req_identifier
作为 cubrid_prepare() 结果的请求标识符。
bind_index
绑定参数的位置。从 1 开始。
bind_value
用于绑定的实际值。
bind_value_type
要绑定的值的类型。(默认情况下省略。因此,系统默认情况下会使用字符串。但是,当它们是 NCHAR、BIT 或 BLOB/CLOB 时,您需要将值的精确类型指定为参数)。
版本 | 描述 |
---|---|
8.3.1 | 添加了 BLOB/CLOB 数据类型支持。 |
示例 #1 cubrid_bind() 示例
<?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("--- 梦幻队 (1992 年美国男子奥运篮球队) ---\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 示例
<?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);
}
?>