PHP mysqli_thread_safe() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_thread_safe() reports whether the MySQLi client library was built thread-safe.

It is typically a procedural function with no connection argument — it describes the library build, not a single open link. Returns true when the client is thread-safe, otherwise false. Most application code never needs to call it.

Syntax

Call mysqli_thread_safe() with the values you need. Start with this line.

PHP
$safe = mysqli_thread_safe();
CallWhat it does
mysqli_thread_safe()Returns true if the MySQLi client library is thread-safe.
var_export(mysqli_thread_safe(), true)Prints true or false for a quick environment check.
mysqli_thread_safe() ? 'ZTS client' : 'non-thread-safe client'Maps the boolean to a short label.

Unlike most mysqli_* functions, thread_safe() does not take a mysqli link. Do not confuse it with mysqli_thread_id(). Thread safety here is about the C client library build flags.

Library declarations
PHP
mysqli_thread_safe(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
trueThe client library is thread-safe.
falseThe client library is not thread-safe.

PHP Examples

Example 1 — Check Thread Safety

Call the function with no arguments and print a fixed demo result.

PHP
<?php
$safe = true; // stand-in sample for mysqli_thread_safe()
echo $safe ? 'thread-safe client: yes' : 'thread-safe client: no', PHP_EOL;

How It Works

  1. Call mysqli_thread_safe() with no link parameter.
  2. Read the boolean return value.
  3. This demo assumes a thread-safe build.
  4. Your machine may print no instead — both are valid builds.

Example 2 — Not the Same as thread_id

Clear up a common naming mix-up.

PHP
<?php
echo 'mysqli_thread_safe(): library build flag (no link)', PHP_EOL;
echo 'mysqli_thread_id($link): server session id', PHP_EOL;

How It Works

  1. thread_safe answers a yes/no build question.
  2. thread_id returns an integer for one connection.
  3. Only thread_id needs an open mysqli link.
  4. Keep the two functions separate in your notes.

Example 3 — Export for Logs

Show a compact environment line.

PHP
<?php
echo 'mysqli_thread_safe=', var_export(true, true), PHP_EOL;

How It Works

  1. Environment dumps sometimes include this boolean.
  2. It helps support staff know the client build type.
  3. It does not replace connection health checks like ping().
  4. Ordinary page controllers can ignore it.

Frequently Asked Questions

Whether the MySQLi client library was compiled as thread-safe.
It describes the library build, not a specific database connection.
No. thread_id returns the server thread ID for a connection; thread_safe is a build flag.
Almost never. It is an environment/diagnostic helper.
true if the client is thread-safe, otherwise false.

Next: mysqli_use_result()

mysqli_use_result() opens an unbuffered result set for the previous query so rows stream f.

mysqli_use_result() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful