普通网友 2018-07-20 14:34
浏览 147

PHP-MySQL-SSH:如何在使用SSH隧道连接时从PHP连接到mysql数据库(托管在CentOS上)

I'm having an issue and I don't know if it's related to the SSH tunnel but here's the deal.

I have a simple function to connect to the database from php and it works fine locally but it doesn't work to connect to a remote server.

connect.php

$dbhost = '172.1.2.3';
$dbuser = 'root';
$dbpass = 'testpass';
$db = 'testdb';

function dbconnect()
{
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $testdb);
$conn->set_charset("utf8");

error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
if(!$conn)
    {
        die('We are unable to connect you to the site! --> '.mysqli_error());
    }
else
    {   
        return $conn;
    }
}

Then, I include this file say on index.php and do the following.

<?php
include 'connect.php';
$conn = dbconnect();
?>

//html content below

the problem I'm having is that when I do the $conn = dbconnect(); the index.php page doesn't load, I just get a blank page. if I comment out $conn = dbconnect(); then the HTML content loads just fine.

I'm not getting error outputs on the console either so I'm a bit clueless as to what the problem might be and I don't know if it's related to the SSH tunneling which I don't know how to include it on the php connection function.

I do however, have to use the SSH credentials to connect to the database from any mysql manager so I want to believe I am missing something in the function.

if it helps, the server is running PHP 5.6 and mysql 5.7.22

Any help is appreciated.

Thanks,

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100