PHP mysqli_ssl_set() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_ssl_set() configures SSL key and certificate paths on a MySQLi handle before the connection is opened.

It must be called before mysqli_real_connect() (after mysqli_init()). The function always returns true; a bad SSL setup shows up later when connect/real_connect fails. It does nothing useful unless OpenSSL support is enabled in PHP.

Syntax

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

PHP
mysqli_ssl_set($mysql, $key, $cert, $ca, null, null);
CallWhat it does
mysqli_ssl_set($link, 'client-key.pem', 'client-cert.pem', 'ca.pem', null, null)Registers client key, cert, and CA file paths before connect.
$mysqli->ssl_set(null, null, '/etc/ssl/certs/ca.pem', null, null)OO form when only a CA file is needed.
mysqli_real_connect($link, $host, $user, $pass, $db, 3306, null, MYSQLI_CLIENT_SSL)Opens the SSL-aware connection after ssl_set().

Lifecycle: mysqli_init() → optional options/set_opt → mysqli_ssl_set() → mysqli_real_connect(). Do not call ssl_set() after the connection is already up. Paths must be readable by PHP.

Library declarations
PHP
mysqli_ssl_set(mysqli $mysql, ?string $key, ?string $certificate, ?string $ca_certificate, ?string $ca_path, ?string $cipher_algos): true

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli handle from mysqli_init().
keyOptional. Path to the private key file, or null.
certificateOptional. Path to the client certificate, or null.
ca_certificateOptional. Path to the CA certificate file, or null.
ca_pathOptional. Directory of trusted CA certificates, or null.
cipher_algosOptional. Allowed cipher list string, or null.

Return Value

Return ValueDescription
trueAlways returns true. Connection/SSL problems appear on real_connect() instead.

PHP Examples

Example 1 — Stage SSL Paths Before Connect

Show the required order with fixed demo lines.

PHP
<?php
foreach ([
    'mysqli_init()',
    'mysqli_ssl_set(...pem paths...)',
    'mysqli_real_connect(... MYSQLI_CLIENT_SSL)',
] as $step) {
    echo $step, PHP_EOL;
}

How It Works

  1. Create a handle with init.
  2. Call ssl_set with certificate paths before connecting.
  3. Open the link with real_connect and SSL client flags as required.
  4. Sample output when connected to a demo database is a successful encrypted handshake.

Example 2 — Always-True Return Value

Explain why a true from ssl_set is not proof the server accepted SSL.

PHP
<?php
$sslSet = true;      // mysqli_ssl_set always true
$connected = false;  // stand-in failed real_connect
echo 'ssl_set=', $sslSet ? 'true' : 'false', PHP_EOL;
echo 'real_connect=', $connected ? 'true' : 'false', PHP_EOL;

How It Works

  1. ssl_set() returning true only means the call was accepted locally.
  2. Bad paths or server SSL policy failures appear on real_connect().
  3. Check connect_error / connect_errno after a failed connect.
  4. Fix certificates and flags, then retry the lifecycle.

Example 3 — OO Method Shape

Object style passes five nullable string arguments.

PHP
<?php
echo '$mysqli->ssl_set($key, $cert, $ca, $capath, $ciphers)', PHP_EOL;
echo 'then $mysqli->real_connect(...)', PHP_EOL;

How It Works

  1. OO and procedural take the same SSL path arguments.
  2. Procedural style inserts the link as the first parameter.
  3. Null means that slot is unused.
  4. Keep secrets (key files) outside the web root.

Frequently Asked Questions

Before mysqli_real_connect(), on a handle from mysqli_init(). After connect is too late.
PHP stages the SSL settings locally. Actual SSL/connect failures are reported by real_connect().
It has no useful effect unless OpenSSL support is enabled in the PHP build.
No. You still need real_connect with the proper SSL-related flags and a server that accepts TLS.
In secure filesystem paths readable by PHP, not in public web directories.

Next: mysqli_stat()

mysqli_stat() returns a single status string from the MySQL server for the current connect.

mysqli_stat() 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