PHP MySQLi 函数

PHP mysqli::thread_safe() / mysqli_thread_safe() 函数告诉客户端库是否编译为线程-安全。

语法

//面向对象风格
public mysqli::thread_safe()

//面向过程风格
mysqli_thread_safe()

参数

无需参数。

返回值

如果客户端库是线程安全的,则返回 true,否则返回 false。

示例:面向对象风格

下面的示例显示了 mysqli 的用法: :thread_safe() 方法。

<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//检查线程是否安全
$result = $mysqli->thread_safe();

if($result){
  print("Thread safety is given.");
}else{
  print("Thread safety is not given.");
}

//关闭连接
$mysqli->close();
?>

上述代码的输出将类似于:

Thread safety is given.

示例:面向过程风格

下面的示例显示了 mysqli_thread_safe() 函数的用法。

<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//检查线程是否安全
$result = mysqli_thread_safe();

if($result){
  print("Thread safety is given.");
}else{
  print("Thread safety is not given.");
}

//关闭连接
mysqli_close($mysqli);
?>

上述代码的输出将类似于:

Thread safety is given.