How to open DB connection in Openshift? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
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... }.

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

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

Related

How to connect MYSQL with PHP

I am stuck after writing all the code, the Website just doesn't connect with mysql at all!
I think this code will help
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
?>
write this code in html file and save it as .php
I hope it works
Create a database in phpmyadmin and then write down below code in a file and save it with .php extension
<?php
$db_username = "db_username"; // Your database login username
$db_password = "db_password"; // Your database login password
$db_name = "db_name"; // The name of the database you wish to use
$db_host = "db_host"; // The address of the database. Often this is localhost, but may be for example db.yoursite.com
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
set those 4 variable as per your db details. mysqli_connect_error() will give you error if the connection will not establish

mysqli_select_db() ERROR, Database couln't connect [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I'm new to PHP and i coded a php page and created a database. I tried to connect db but it's not connecting.
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
Hi Try to debug and echo bugs in connection with mysqli_error() function.
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
You are mixing mysql and mysqli
You have to replace
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
With this code
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
connection variable then database name ..try this
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
and mysql close should be like this
mysqli_close($mysqlconn);
dont mix mysql with mysqli
You should write
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
This will solve Your Problem

php/mysql connection error(with config file)

I am developing a login/register system for a website but when i'm trying to connect to the database it gives this error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\register.php on line 17
I'm using a external config file wich is been imported.
My config file:
// Connection details
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
My connection code:
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
I found one solution that worked to connect but then my config file was useless.
No config but only this:
// Create connection
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
If someone nows a solution where i can keep using my config file please post it here.
Thanks in advance.
Well, if the connection works with this values:
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
But not with this values:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "cloud";
Why don't you just set the "working" values into your config file. So it would look like this:
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
Another solution which is described here includes the creation of a new user, so you don't connect via root, but the new created user.

Wamp, PHP MySQL - Connecting error

I've installed a WAMP server. Now I want to connect to a MySQL database using php. I have one problem you need to get the username.
This is the code I use. Its from php.net http://php.net/manual/en/mysqli.quickstart.connections.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "password";
$db_name = "test";
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
I get the following error:
Warning: mysqli::mysqli(): in C:\wamp\www\Atletieker\dbconnect.php on line 7
Failed to connect to MySQL: (1045) Accès refusé pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
Warning: main(): Couldn't fetch mysqli in C:\wamp\www\Atletieker\dbconnect.php on line 11
How do I get the username of my database?
Try this...
$db_host = "127.0.0.1";
$db_username = "root";
$db_password = "";
$db_name = "test";
Obviously the $db_password should be empty, if you haven't changed any settings the default password should be blank.

How to connect to mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do i write the connection string to connect to mysql database with this info:
Using php connection string
Database Details:
phpmyadmin url: https://medicalng.com/phpmyadmin
host: localhost
database: wlfmedic_ptest1
username: wlfmedic_ptest1
password: Prog#te$t104
Use mysqli. mysqli connection is like this:
$host = "localhost";
$database = "wlfmedic_ptest1";
$username = "wlfmedic_ptest1";
$password = 'Prog#te$t104';
$connection = mysqli_connect($host, $username, $password, $database);
if(mysqli_connect_errno()){ //To show error if fails to connect
die(mysqli_connect_error());
}
Something basic, not secure tho.
<?php
mysql_connect(localhost, ##YOUR USERNAME##, ##YOUR PASSWORD##);
mysql_select_db('##YOUR DATABASE NAME##');
$result = mysql_query("SELECT * FROM emails");
?>
and updated mysqli http://www.sanwebe.com/2013/03/basic-php-mysqli-usage
<?php
$db_hostName="localhost";
$databaseName= "wlfmedic_ptest1";
$db_userName= "wlfmedic_ptest1";
$db_password= "Prog#te$t104";
$con=mysql_connect($db_hostName,$db_userName,$db_password);
if(!$con){
die ("Could Not successfully with DataBase".mysql_error());
}else{
echo "Connect connect with succssfully .";
}
$db=mysql_select_db($db_databaseName,$con);
if(!$db)
{
echo "Can notConnect with Bd Social";
}
else
{
echo "Conncet With Bd Social";
}
?>
try this ..
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found ";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
?>
also you can refer this link

Categories