When using PHP's mysqli extension to connect to a MySQL database, encountering connection failures is a common scenario. Normally, we determine the specific cause by checking the connection error code and error message. However, there are times when we also want to retrieve some server information to aid in troubleshooting—such as the MySQL server version.
This article explains how to use the mysqli class’s $server_info property to retrieve server details after a successful connection, and how to handle errors when connections fail, helping you better understand your database connection status.
First, here's the basic flow for a successful database connection:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$mysqli</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title function_ invoke__">mysqli</span></span><span>(</span><span><span class="hljs-string">'gitbox.net'</span></span><span>, </span><span><span class="hljs-string">'username'</span></span><span>, </span><span><span class="hljs-string">'password'</span></span><span>, </span><span><span class="hljs-string">'database'</span></span><span>);
<p></span>if ($mysqli->connect_error) {<br>
die('Connection failed. Error code: ' . $mysqli->connect_errno . ', Error message: ' . $mysqli->connect_error);<br>
}</p>
<p>echo 'Successfully connected to MySQL server. Version: ' . $mysqli->server_info;</p>
<p>$mysqli->close();<br>
?><br>
</span>
Here, $mysqli->server_info returns the server version, such as 8.0.31.
It’s important to note that $server_info is only available after a successful connection. If the connection fails—due to network issues or authentication errors—$server_info won’t return anything, because no valid connection has been established.
Therefore, if you want to retrieve server information during a failure scenario, you must first ensure the connection is successful or try alternative methods (like pinging the server or using other APIs) to aid diagnosis.
Here’s an example that combines error detection and server info retrieval:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$mysqli</span></span><span> = @</span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title function_ invoke__">mysqli</span></span><span>(</span><span><span class="hljs-string">'gitbox.net'</span></span><span>, </span><span><span class="hljs-string">'username'</span></span><span>, </span><span><span class="hljs-string">'password'</span></span><span>, </span><span><span class="hljs-string">'database'</span></span><span>);
<p></span>if ($mysqli->connect_errno) {<br>
// Connection failed, output error code and message<br>
echo 'Connection failed. Error code: ' . $mysqli->connect_errno . '<br>';<br>
echo 'Error message: ' . $mysqli->connect_error . '<br>';<br>
} else {<br>
// Connection successful, retrieve server info<br>
echo 'Successfully connected to MySQL. Server version: ' . $mysqli->server_info;<br>
}</p>
<p>$mysqli->close();<br>
?><br>
</span>
Here, the @ symbol suppresses warnings during object construction. Errors are then determined using $mysqli->connect_errno.
mysqli::$server_info is only available after a successful connection. Otherwise, it will throw an error or return null.
When the connection fails, always check $mysqli->connect_errno and $mysqli->connect_error first to determine the cause.
If you want to understand the server state when the connection fails, consider using network tools like ping, telnet, or inspecting server logs.
Use exception catching and proper error-handling logic to make your application more robust and improve user experience.
With this approach, you can clearly catch and output error details when a connection fails, and retrieve server version and other information upon success to help diagnose and resolve issues efficiently.