在Openshift中使用PHP MySQL 5.5 db连接

时间:2014-02-26 14:54:49

标签: php mysql git openshift

我对openshift和这些git,ssh编程很新。到目前为止我开发了这么多。 安装PHP 5.3,MySQL 5.5,phpMyAdmin 4.0墨盒

这是我在php中的dbcon文件。 基本上我需要将我的php应用程序与openshift mysql db连接。这就是我所做的。

<?php

// Database Connection Setting

$dbhost = "127.0.0.1"; // Host name 
$dbport = "3308"; // Host port
$dbusername = "user"; // Mysql username 
$dbpassword = "pass"; // Mysql password 
$db_name = "mf"; // Database name 

$mysqlCon = mysqli_connect($dbhost, $dbusername, $dbpassword, "", $dbport) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, $db_name) or die("Error: " . mysqli_error($mysqlCon));
?>

这给了我一个关于openshift的错误,但适用于其他php应用程序。我的错误说明只是错误:{...空格...}。

你可以帮助我吗?

2 个答案:

答案 0 :(得分:15)

非常感谢你们的努力。之前没有抛出视觉错误。但是我已经通过这样做了它。

**

全球使用

**

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"); // Host name 
$dbport = constant("DB_PORT"); // Host port
$dbusername = constant("DB_USER"); // MySQL username 
$dbpassword = constant("DB_PASS"); // MySQL password 
$db_name = constant("DB_NAME"); // Database name 

**

替代地

**

$dbhost = getenv('OPENSHIFT_MYSQL_DB_HOST'); // Host name 
$dbport = getenv('OPENSHIFT_MYSQL_DB_PORT'); // Host port
$dbusername = getenv('OPENSHIFT_MYSQL_DB_USERNAME'); // MySQL username 
$dbpassword = getenv('OPENSHIFT_MYSQL_DB_PASSWORD'); // MySQL password 
$db_name = getenv('OPENSHIFT_GEAR_NAME'); // Database name 

答案 1 :(得分:4)

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_APP_NAME'));

$mysqlCon = mysqli_connect(DB_HOST, DB_USER, DB_PASS, "", DB_PORT) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, DB_NAME) or die("Error: " . mysqli_error($mysqlCon));

或者更好的是,只需在连接字符串中使用环境变量:

$mysqlCon = mysqli_connect(getenv('OPENSHIFT_MYSQL_DB_HOST'), getenv('OPENSHIFT_MYSQL_DB_USERNAME'), getenv('OPENSHIFT_MYSQL_DB_PASSWORD'), "", getenv('OPENSHIFT_MYSQL_DB_PORT')) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, getenv('OPENSHIFT_APP_NAME')) or die("Error: " . mysqli_error($mysqlCon));