PDO
7 methods表示 PHP 与数据库服务器之间的连接,提供预处理语句和事务。
array_push(array &$array, mixed ...$values): int创建一个 PDO 实例,表示通过 DSN 到数据库的连接。
Parameters
| Name | Type | Description |
|---|---|---|
| dsn | array | 数据源名称(例如 'mysql:host=localhost;dbname=test')。 |
| username | mixed | 数据库用户名。 |
Returns
int
Example
php
<?php
$arr = [1, 2];
$n = array_push($arr, 3, 4);
// $arr == [1, 2, 3, 4], $n == 4array_pop(array &$array): mixed|null执行 SQL 语句,以 PDOStatement 形式返回结果集。用于非参数化查询。
Parameters
| Name | Type | Description |
|---|---|---|
| query | array | 要执行的 SQL 语句。 |
Returns
mixed|null
Example
php
<?php
$arr = [1, 2, 3];
$v = array_pop($arr);
// $v == 3, $arr == [1, 2]count(Countable|array $value, int $mode = COUNT_NORMAL): int为执行准备语句并返回一个 PDOStatement 对象。用于参数化查询。
Parameters
| Name | Type | Description |
|---|---|---|
| query | Countable|array | 带占位符的 SQL 语句。 |
| options | int | 驱动程序选项。 |
Returns
int
Example
php
<?php
$arr = [1, 2, 3];
echo count($arr); // 3array_map(?callable $callback, array $array, array ...$arrays): array关闭自动提交模式并开始一个事务。在调用 commit() 之前,更改不会被提交。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | ?callable | Function to apply (null = array of values). |
| array | array | Input array. |
Returns
array
Example
php
<?php
$squares = array_map(fn($x) => $x * $x, [1, 2, 3]);
// [1, 4, 9]array_filter(array $array, ?callable $callback = null, int $mode = 0): array提交一个事务,使自 beginTransaction() 以来的所有更改永久生效。
Parameters
| Name | Type | Description |
|---|---|---|
| array | array | Input array. |
| callback | ?callable | Predicate (default removes falsy values). |
Returns
array
Example
php
<?php
$evens = array_filter([1, 2, 3, 4], fn($x) => $x % 2 === 0);
// [1 => 2, 3 => 4] (keys preserved)array_merge(array ...$arrays): array回滚当前事务,撤销自 beginTransaction() 以来所做的所有更改。
Parameters
| Name | Type | Description |
|---|---|---|
| arrays | array | Arrays to merge. |
Returns
array
Example
php
<?php
$r = array_merge([1, 2], [3, 4]);
// [1, 2, 3, 4]
$m = array_merge(["a" => 1], ["a" => 2, "b" => 3]);
// ["a" => 2, "b" => 3]in_array(mixed $needle, array $haystack, bool $strict = false): bool返回最后插入行的 ID,或序列中的最后一个值。
Parameters
| Name | Type | Description |
|---|---|---|
| name | mixed | 序列名称(某些驱动程序如 PostgreSQL)。 |
| haystack | array | Array to search. |
| strict | bool | If true, uses === comparison. |
Returns
bool
Example
php
<?php
$ok = in_array(2, [1, 2, 3]); // true
$ok2 = in_array("2", [1, 2, 3], true); // false (strict)