Reputation: 21
i have upload my web application to OpenShift. my application is using the PHP and MySQL Cartridges.
i have created a few databases using PHPmyAdmin (Through the openshift web interface). each database has it's own unique name obviously. in my PHP code i initialize the connection to one of my MySQL databases in this fashion:
$mysql_database=<some data base>;
$connection = mysqli_connect(getenv('OPENSHIFT_MYSQL_DB_HOST'), getenv('OPENSHIFT_MYSQL_DB_USERNAME'), getenv('OPENSHIFT_MYSQL_DB_PASSWORD'), "$mysql_database", getenv('OPENSHIFT_MYSQL_DB_PORT')) or die("Error: " . mysqli_error($connection));
mysql_set_charset("utf8", $connection);
then somewhere else i am doing some queries from my PHP code (like select , update and so on) on tables inside the database i connected to. for example:
$query_run=mysql_query("SELECT * from `$some_table` WHERE `id`='$id'")
the connection itself does not fail on anything, however all my queries fail on :
Access denied for user ''@'localhost' (using password: NO)
When i manually issue these commands on the actual machine everything runs fine. so this works: SSHing into my machine using PuTTy , and doing :
mysql -u $OPENSHIFT_MYSQL_DB_USERNAME -h $OPENSHIFT_MYSQL_DB_HOST -P $OPENSHIFT_MYSQL_DB_PORT -p <some data base>
mysql>SELECT * from `<some table>` WHERE `id`='<some id>'
i did not modify the security settings of anything, neither did i change the username / password that were generated for me for the MySQL actions.
also, everything runs perfectly fine on my local application running on a xampp Apache+MySQL server.
any ideas?
Upvotes: 0
Views: 955
Reputation: 4002
You shouldn't really be using mysql_query()
like that (see Is this query safe from sql injection?)
You can use a much better abstraction: PHP Data Objects (PDO). e.g.
try {
// Set your connection variables
$host = getenv('OPENSHIFT_MYSQL_DB_HOST');
$port = getenv('OPENSHIFT_MYSQL_DB_PORT');
$username = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$password = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$dbname = 'database_name';
// Create the connection string
$conn_str = "mysql:host=$host;port=$port;dbname=$dbname"
// And create a db handler with PDO
$dbh = new PDO($conn_str, $username, $password);
// Prepare whatever statement, could also be SELECT etc
$stmt = $dbh->prepare("INSERT INTO TABLE_NAME (name, value) VALUES (?, ?)");
// Binding parameters like this prevents SQL Injection
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// Assign values to the variables
$name = 'Random';
$value = 1;
// Finally, execute the statement
$stmt->execute();
}
catch(PDOException $e) {
// Print out whatever error we got
echo $e->getMessage();
}
// Don't forget to close the connection
$dbh = null
Not only this is a better solution for handling MySQL databases with PHP, but hopefully it will also fix your authentication problems (or make them easier to debug, at least).
Upvotes: 2
Reputation:
Stop and Start your application (not a restart). Sometimes apache needs to be restarted to pick up the new mysql environment variables.
Upvotes: 0