如何将OpenShift PHP连接到MySQL数据库?

时间:2016-03-09 10:44:48

标签: php mysql openshift

我仍然非常擅长在线上传php和mysql。这是我上传我的php文件http://studcams-shadowj3yz.rhcloud.com/main.php

的链接

它包含config.phpconnection.php,以下是代码

config.php

define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));

connection.php

$this->connect = mysqli_connect(constant("DB_HOST"), constant("DB_USER"), constant("DB_PASS"), constant("DB_NAME"), constant("DB_PORT"))
        or die("Could not connect to db");

这就是我与openshift连接的方式吗?它显示Could not connect to db。我该如何解决这个问题?谢谢!

编辑:

好的,现在我已将代码更改为

define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
        define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
        define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
        define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
        define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));

        $dbhost = constant("DB_HOST");
        $dbport = constant("DB_PORT");
        $dbusername = constant("DB_USER");
        $dbpassword = constant("DB_PASS");
        $db_name = constant("DB_NAME");

        $this->connect = mysqli_connect($dbhost, $dbusername, $dbpassword, "", $dbport) or die ("Connect Error: ".mysqli_error($this->connect));
        mysqli_select_db($this->connect, $db_name) or die("DB Error: ".mysqli_error($this->connect));

现在显示Connect Error:,但没有其他信息。

0 个答案:

没有答案