6

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... }.

Dharman
  • 30,962
  • 25
  • 85
  • 135
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
  • What is the error you get? – Gerben Jacobs Feb 26 '14 at 15:00
  • does the user have access rights to all db's, or just the _mf_ db? Also, pass `$db_name` as a fourth argument, that way, you can do away with the `mysqli_select_db` call – Elias Van Ootegem Feb 26 '14 at 15:01
  • Have you enabled [PHP Error Debugging](http://stackoverflow.com/a/845025/2752041)? – mathielo Feb 26 '14 at 15:01
  • I didn't get any visual error. Just an string saying "Error: {white space}". – CodeMonkey Feb 26 '14 at 15:05
  • Yeah, dig some more and tried a solution. it works. i put it here to let know other's who new to openshift. – CodeMonkey Feb 26 '14 at 15:06
  • @Inuka so you placed this and already knew the answer? It look like a link to commercial site. As far as, I know that is not allowed. – Mr. Radical Feb 26 '14 at 15:07
  • No no.. nothing like that. tried so hard to find the solution. so came here for professionals. but i just found a fix from another stackoverflow question, tried it and it worked. so i put my findings here and let you guys also. – CodeMonkey Feb 26 '14 at 15:10
  • It's a development environment. you can test your cloud app in http://openshift.redhat.com. I tried my experiments. came up with an error. – CodeMonkey Feb 26 '14 at 15:11

2 Answers2

16

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 
Dharman
  • 30,962
  • 25
  • 85
  • 135
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
  • 1
    Why not just use the constants in your connection instead of assigning them to regular variables first? Seems like an extra step that does not need to be taken. –  Feb 26 '14 at 15:11
  • ooh.. yeah that's right. I'm just an intermediate in php. put it here so simply to understand others. your comment will help other's to develop from that on. Thanks @developercorey. – CodeMonkey Feb 26 '14 at 15:18
  • @matrixwebtech running the app from local/remote server and keeping the db on a different server? I'm not 100% sure Openshift gives `MySQL` access via web sockets. I read a post that we can configure it. If you have a C# app configured to run on openshift then you'll need that connection settings for the db connection. this is only `PHP` connection. – CodeMonkey May 14 '17 at 04:36
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));

Or better still, just use the environment variables in your connection string:

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