All HowTo's MySQL & MariaDB

Interacting with MySQL using PHP with OpenShift

This article shows how to use PHP to access a database from within OpenShift. In this case the PHP code is accessing the MySQL database within the same application.

function db_connect()
        $db_username = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
        $db_password = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
        $db_host = getenv('OPENSHIFT_MYSQL_DB_HOST') . ":" . getenv('OPENSHIFT_MYSQL_DB_PORT');
        $db_name = "mydbname";
        $conn = new mysqli($db_host, $db_username, $db_password, $db_name);
        return $conn;

The above function should be called before you run any SQL commands.

Leave a Reply

Your email address will not be published. Required fields are marked *