首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]CollectionFind::bind()函数—用法及示例

发布于 2025-04-21 16:42:19
0
46

没有可用的版本信息,可能只有在Git中

说明

public mysql_xdevapi\CollectionFind::bind(array $placeholder_values): mysql_xdevapi\CollectionFind

它允许用户在查找操作的搜索条件中将参数绑定到占位符。占位符的形式为:NAME,其中':'是必须始终存在于任何NAME之前的通用前缀,NAME是占位符的实际名称。如果必须在搜索条件中替换多个实体,则bind函数接受占位符列表。

参数

placeholder_values

要在搜索条件中替换的值;允许多个值,并作为一个数组传递,其中“PLACEHOLDER_NAME => PLACEHOLDER_VALUE”。

返回值
一个CollectionFind对象,或者与execute()连接以返回一个Result对象。

示例
示例 #1 mysql_xdevapi\CollectionFind::bind() example

<?php
$session = mysql_xdevapi\getSession("mysqlx://user:password@localhost");
$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema = $session->getSchema("addressbook");
$create = $schema->createCollection("people");
$result = $create
  ->add('{"name": "Alfred", "age": 18, "job": "Butler"}')
  ->execute();

// ...

$collection = $schema->getCollection("people");

$result = $collection
  ->find('job like :job and age > :age')
  ->bind(['job' => 'Butler', 'age' => 16])
  ->execute();

var_dump($result->fetchAll());
?>

以上示例的输出类似于:

array(1) {
  [0]=>
  array(4) {
    ["_id"]=>
    string(28) "00005b6b536100000000000000cf"
    ["age"]=>
    int(18)
    ["job"]=>
    string(6) "Butler"
    ["name"]=>
    string(6) "Alfred"
  }
}
评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流