普通网友 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 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧