database connection on openshift using php - php

I have used this code to connect to a database on localhost. What PHP code should I use to connect a PHP application to a database on openshift?
$user_name = "root";
$password = "";
$database = "project";
$server = "127.0.0.1";
$db_handle=mysql_connect($server, $user_name, $password);
$db_found=mysql_select_db($database,$db_handle);

You can use environment variables which are placeholders for values that are provided to a software program at runtime:
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'));
$dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$dbh = new PDO($dsn, DB_USER, DB_PASS);

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

Php login to your database

Is there a way to connect to your database without having to always type:
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "pass123";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Is it possible to have a file that always connects to it?
As suggested in comment,
You need to make a connection.php file and add your connection code into it.
connection.php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "*****";
$dbName = "LoginDatabase";
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
In this file you have $db object.
in other PHP file, write
include 'connection.php';
In that file you can directly access $db object.
Suggestion: You should also check db connection error in your connection.php file. So if you have any connection error then you can fetch it.
instead of writing this line directly
$db = new PDO("mysql:dbname=$dbName;host=$dbHost;port=3306", $dbUser, $dbPass);
Use below code to check error also.
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$db = new PDO($dsn,$dbUser,$dbPass, $opt);
For more detail, Refer this PDO tag wiki.

I can not get mysqli_connect to work using Xampp

I want to use mysqli_connect and I have tried all kinds of variations and none of them have work. They all throw "No database selected". I am having a configuration problem or a code error? THIS WORKS
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysql_connect($host, $uname, $pwd);
$database = mysql_select_db($database, $conn)
I don't know to make this question any clearer,,,I want to use
THESE DO NOT WORK
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysqli_connect($host, $uname, $pwd);
$database = mysqli_select_db($database, $conn);
or
$host = "localhost";
$uname = "root";
$pwd = "";
$database = "testdb";
$conn = mysqli_connect($host, $uname, $pwd, $database);
but it is NOT WORKING, I want to know why it is not working and how to make it work using Xampp. It is most likely a configuration problem but I am lost. I have asked this question before there doesn't seem to be anyone that knows how to fix.
$database = mysqli_select_db($database, $conn);
Is the incorrect syntax, it's
mysqli_select_db($conn, $database);
Additionally, you could delete that line and do the following:
$conn = mysqli_connect($host, $uname, $pwd, $database);
I have tested this on my live server
$conn = mysqli_connect($host, $uname, $pwd, $database);
There is definitely a configuration problem and I will check further.

Connect PHP with mysql database using mysql_connection

i need to know how to declare the database connection using mysql_connection in php
and this is sample of code
$password = "";
$localhost = "localhost";
$username = "root";
$this->connection = mysql_connect($this->$localhost,$this->$username,$this->$password);
Note i am using wamp server
Use mysqli instead. Start like this:
$db = new MySQLi("host", "username", "password", "db");
if(!$db)
{
die("your_error_msg" . mysqli_error($db));
}
$db->set_charset("utf8");

Connect to MySQL on Amazon AWS RDS

I typically connect using the following:
$db_host = "localhost";
$db_username = "bill";
$db_pass = "mypassword";
$db_name = "theDB";
mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");
I tried changing the host to the RDS endpoint below with no luck:
$db_host = "mysql7.1234567890123.us-west-2.rds.amazonaws.com";
-The security group has inbound rules that allow my IP.
-I've tried adding "10.0.0.0/8" to the security group from Connecting From PHP Server to MySQL Server on Amazon AWS
Other info: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP_legacy.rds.html
There are couple of things and use PDO or mysqli to connect. If you've made sure that you can connect to RDS by setting the right security group, then you've got the main issues squared away.
Connect to Amazon RDS with this in PHP:
$dbhost = $_SERVER['RDS_HOSTNAME'];
$dbport = $_SERVER['RDS_PORT'];
$dbname = $_SERVER['RDS_DB_NAME'];
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
$username = $_SERVER['RDS_USERNAME'];
$password = $_SERVER['RDS_PASSWORD'];
$dbh = new PDO($dsn, $username, $password);
$link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);
Example 1. Example using PDO to connect to an RDS database
$dsn = 'mysql:host=mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com;port=3306;dbname=mydb';
$username = 'sa';
$password = 'mypassword';
$dbh = new PDO($dsn, $username, $password);
Example 2. Example using mysqli_connect() to connect to an RDS database
$link = mysqli_connect('mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com', 'sa', 'mypassword', 'mydb', 3306);
if nothing works, launch a mysql client like navicat or mysql workbench (free) and punch in all the necessary fields and try to connect and see if it loads the db.
This ended up working for me. Thanks for you help!
$db_host = 'instance2.123456789.us-west-2.rds.amazonaws.com:3306'; //RDS Endpoint...
$db_username = 'myusername';
$db_pass = 'mypassword';
$db_name = 'myDB';
mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");

Categories