Home >Backend Development >PHP Tutorial >How to Resolve \'mysqli_connect() expects parameter 6 to be string, resource given\' Error when Connecting to MySQL Over SSH in PHP?
You've encountered an error while connecting to your database using SSH and the ssh2 library. Specifically, the error is "mysqli_connect() expects parameter 6 to be string, resource given."
The above error message suggests that you've passed an incorrect type to the sixth parameter of the mysqli_connect function.
To resolve the issue, pass a string instead of a resource as the sixth parameter. In your case, this parameter is expected to be the path to the SSH tunnel socket.
To connect to a MySQL server over SSH, you'll need to set up an SSH tunnel using the following steps:
1. SSH Tunnel Command:
ssh -fNg -L 3307:10.3.1.55:3306 [email protected]
2. MySQL Client Connection:
Once the tunnel is set up, you can connect to your MySQL server using your local MySQL client:
mysql -h 127.0.0.1 -P 3307 -u dbuser -p passphrase
3. PHP Connection:
Finally, connect to your MySQL server in your PHP application:
<code class="php"><?php $smysql = mysql_connect("127.0.0.1:3307", "dbuser", "passphrase"); mysql_select_db("db", $smysql); ?></code>
Security Note:
For enhanced security, use a bastion host (Jumpbox) as the SSH proxy instead of connecting directly to your MySQL server.
The above is the detailed content of How to Resolve \'mysqli_connect() expects parameter 6 to be string, resource given\' Error when Connecting to MySQL Over SSH in PHP?. For more information, please follow other related articles on the PHP Chinese website!