MongoLab addon PHP app hosted on heroku - php

I've a PHP app to be hosted on heroku. The basic functionality of the app is to collect some data from users (through browsers) & store those in a database (since this is a PHP app, the database modifications must be made through PHP only). Now I've enabled the MongoLab addon from heroku control panel & created database, user, & sample collection into that database. I've deployed my code to heroku via git. Following is the content of a file db.php located inside the root folder of the app
<?php
try {
$user = 'my user name';
$pass = 'my password';
$app = 'my app name';
$col = 'sample collection';
echo 'connecting ...';
$connection = new Mongo('mongodb://'.$user.':'.$pass.'#ds034512.mongolab.com:34512/'.$app);
echo 'connected';
$database = $connection->selectDB($app);
$collection = $database->selectCollection($col);
}
catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
?>
now whenever I'm trying to execute the file on the heroku server itself (by caling the url http://my-app-name.herokuapp.com/db.php from my local browser) it's showing
"connecting ..." & in the console there is an error message "NetworkError: 500 Internal Server Error - http://my-app-name.herokuapp.com/db.php"
Can anyone suggest me any idea on how this database integration be done for apps hosted on heroku ?

You didn't include the mongodb driver. This gist has all the instructions.
https://gist.github.com/1288447

Related

Connecting php and mysql database post production in web development

I wrote a simple user registration system that runs on my local machine using apache. (Localhost at port 8080).
My php file that tries to connect to mysql database looks like :
<?php
$con = mysqli_connect("localhost","root","","register");
//localhost will have to change to host name if hosted on different server... I think.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Everything works and I can register/sign in a user on my local machine.
I know that I have to change $con = mysqli_connect("localhost","root","","register"); once I am ready to upload my site to host it in a free server like surge.sh or github pages.
I pushed my site to github pages from my repository and when I click "Register" / "Sign in" it doesn't load the page instead it acts as if I want to download the .php file. How should I make the changes? I need some help and suggestions.

How to connect to a SQL Database-s2 from a .php application in BlueMix

So I have created a www site on my private server. It's basically a PHP + javascript page. Now I need to move it to BlueMix. The only problem is that while I used a MySQL db on my private server now I need to use SQL Database-s2 and all (both my php page and the db) on BlueMix.
Mike
You have to parse db information from VCAP_SERVICES\service credentials.
If your database service is "SQL Database" you can connect to SQLDB using this example code:
//parse VCAP_SERVICES Environment variable
$vcap_services = $_ENV["VCAP_SERVICES"];
$services_json = json_decode($vcap_services,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
echo "No sqldb service instance is bound. Please bind a sqldb service instance";
return;
}
//Get Credentials object (db,host,port,username,password)
$sqldb_config = $services_json["sqldb"][0]["credentials"];
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$sqldb_config["db"].
";HOSTNAME=".
$sqldb_config["host"].
";PORT=".
$sqldb_config["port"].
";PROTOCOL=TCPIP;UID=".
$sqldb_config["username"].
";PWD=".
$sqldb_config["password"].
";";
$conn = db2_connect($conn_string, '', ''); //db connection
the information is automatically retrieved from the VCAP_SERVICES.
The application read the env. variable and retrieves automatically the username,host,db password field.
but you can manually set db, host, port, username, password looking at
service credentials of the service:
How to retrieve credentials information:
from DASHBOARD UI
click on your service icon in the dashboard UI
in the new page, in the left menu, click on "Service Credentials"
If you don't see any credentials but you see the button "ADD CREDENTIALS", click on this button, edit the name credentials as you want e click on "ADD" button.
In this way you will see the service information (credentials)
from CF command line
cf env your_app_name
In the code we are using DB2 module: use the following buildpack https://github.com/ibmdb/db2heroku-buildpack-php when you are using SQLDB with PHP. It will install the 'ibm_db2' php module for your use.
You can set the buildpack when you push your application on Bluemix:
cf push <your_app_name> -b https://github.com/ibmdb/db2heroku-buildpack-php
You can deploy your application on Bluemix using the PHP buildpack. After creating the SQL Database instance and binding it to the PHP application on Bluemix, to connect to the SQLDB service in Bluemix you can use db2_connect with the credentials retrieved from the VCAP_SERVICES environment variable. I don't know your business requirements, however if you want to keep consistency across the environments and don't want to migrate to another RDBMS you could try ClearDB, that is a reliable, fault tolerant, geo-distributed database-as-a-service for your MySQL powered applications.

GAE PHP Lose connection with mongodb

I am building PHP app with codeigniter and using GAE with Mongodb. GAE is billing enabled but the connection to mongo is not stable and it drops 1/4 when the app is online, locally works fine with GAE PHP SDK:
php.ini
google_app_engine.enable_functions = "libxml_disable_entity_loader"
google_app_engine.enable_curl_lite = “1”
extension="mongo.so"
mongo_library.php
try {
//connect to the mongodb server
SELF::$mongo_client = new MongoClient($config_data['mongo_connection_string']);
//select the mongodb database
$this->db = SELF::$mongo_client->selectDB($config_data['mongo_database']);
} catch (MongoConnectionException $exception) {
//if mongodb is not connect, then display the error
show_error('Unable to connect to Database', 500);
}
I am using mongo free plan from mongolab.com google cloud provider.
Here is the link where is show My app and where you can see how it fails by refreshing couple of times.
$a = mongo_db::$mongo_client;
$connections = $a->getConnections();
print_r($connections);
Here is the issue I opened:
https://code.google.com/p/googleappengine/issues/detail?id=12392
and this issue/bug is already fixed.

How do I connect to external mysql database on heroku?

I created a FB app hosted on Heroku.
Instead of Heroku's database I use a mysql database. I made the chage using this code:
heroku config:add DATABASE_URL=mysql://user:pass#server:port/database_name
So far everything is ok but now I have an issue connecting to the database in my index.php file.
I don't know ho to do.
I tried to connect to the DB as I do on my website
try {
$bdd = new PDO('mysql:host=host;dbname=database_name', 'user', 'pass');
}
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
But I have an error in my app:
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
Thank you
Check whether your remote database port is opened for external connection. Look for bind-address configuration of your remote mysql server.

Connect to MongoDB on Appfog from localhost

I'm trying to run my application on my local pc. Everything looks perfectly fine on the cloud, it works and connects to MongoDB with the code below:
$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mongo_config = $services_json["mongodb-1.8"][0]["credentials"];
// Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = $mongo_config["hostname"];
// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = $mongo_config["port"];
// The database you want to work from (required)
$config['mongo_db'] = $mongo_config["db"];
// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = $mongo_config["username"];
$config['mongo_pass'] = $mongo_config["password"];
But since I cannot get the vcap_services from my local pc, I'm using constant values for the connection to connect my app to the remote mongo server on appfog:
// Generally will be localhost if you're querying from the machine that Mongo is installed on
$config['mongo_host'] = "***";
// Generally will be 27017 unless you've configured Mongo otherwise
$config['mongo_port'] = "***";
// The database you want to work from (required)
$config['mongo_db'] = "db";
// Leave blank if Mongo is not running in auth mode
$config['mongo_user'] = "***";
$config['mongo_pass'] = "***";
But when I try to execute the app through http://localhost/my-app, it gave me this error:
Unable to connect to MongoDB: Failed to connect to: ***: Connection timed out
What can be the problem?
* values are deleted for the privacy.
Did you try connecting without the VCAP variables not on localhost but the actual server? AppFog may not give authorization to connect without VCAP variables or something may be wrong on the values you manually get.
I believe this is an authorization problem. Try another cloud service if you cannot handle with it. This is not the only solution.

Categories