略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: PDO::sqliteCreateCollation

2024-11-14

PDO::sqliteCreateCollation

(PHP 5 >= 5.3.11, PHP 7, PHP 8)

PDO::sqliteCreateCollation Registers a User Defined Function for use as a collating function in SQL statements

说明

public PDO::sqliteCreateCollation(string $name, callable $callback): bool
警告

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的 PHP 发布版本中未通知就被修改。使用本函数风险自担。

参数

name

Name of the SQL collating function to be created or redefined.

callback

The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the collation. It should accept two strings and return as strcmp() does, i.e. it should return -1, 1, or 0 if the first string sorts before, sorts after, or is equal to the second.

This function need to be defined as:

collation(string $string1, string $string2): int

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 PDO::sqliteCreateCollation() example

<?php
$db 
= new PDO('sqlite::memory:');
$db->exec("CREATE TABLE test (col1 string)");
$db->exec("INSERT INTO test VALUES ('a1')");
$db->exec("INSERT INTO test VALUES ('a10')");
$db->exec("INSERT INTO test VALUES ('a2')");

$db->sqliteCreateCollation('NATURAL_CMP''strnatcmp');
foreach (
$db->query("SELECT col1 FROM test ORDER BY col1") as $row) {
  echo 
$row['col1'] . "\n";
}
echo 
"\n";
foreach (
$db->query("SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP") as $row) {
  echo 
$row['col1'] . "\n";
}
?>

以上例程会输出:

a1
a10
a2

a1
a2
a10
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/pdo.sqlitecreatecollation.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3