Skip to content
Advertisement

How to open DB connection in Openshift? [closed]

This is my dbcon file in PHP. Basically, I need to connect my PHP application with openshift MySQL DB. Here’s what I did.

<?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));
?>

This gives me an error on openshift but works for other PHP apps. I get nothing on error explanation only as Error: { …blank space… }.

Advertisement

Answer

I’ve made it to work by doing this.

Global Use

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 

Alternatively

$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 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement