I am unable to connect to my database residing on dotCloud. I tried:
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
and
$mysqli = mysqli_connect($db_host, $db_user, $db_password, $db_name);
and
$mysqli = new mysqli($remote_server, $db_user, $db_password, $db_name);
and
$mysqli = mysqli_connect($remote_server, $db_user, $db_password, $db_name);
but it fails to connect, and I get "Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data."
I retrieve variables dynamically above the mysqli script with the following:
$env = json_decode(file_get_contents("/home/dotcloud/environment.json"));
$db_user = $env->DOTCLOUD_DB_MYSQL_LOGIN;
$db_password = $env->DOTCLOUD_DB_MYSQL_PASSWORD;
$db_host = $env->DOTCLOUD_DB_MYSQL_HOST;
$db_port = $env->DOTCLOUD_DB_MYSQL_PORT;
$remote_server = '$db_host:$db_port';
//I also define $db_name here
$db_name = 'mydbname';
I also have the following code below the mysqli script:
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
$result["status"] = "failed";
$result["message"] = "Failed to connect to database.";
echo json_encode($result);
exit;
} else {
// Successfully connected!
$stmt = $mysqli->stmt_init();
echo "<p>Successfully connected!!</p>";
}
What am I doing wrong?
There are a couple of things wrong in your code.
1. Your $remote_server variable is using a single quote
$remote_server = '$db_host:$db_port';
This means that $remote_server will not expand the $db_host and $db_port variables. You should use double quotes. If you used the variable as it is, it wouldn't work for you.
$remote_server = "$db_host:$db_port";
See this page for more info:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
2. You are not using the mysql port when connecting, which is required on dotCloud since it doesn't run mysql on the standard port of 3306.
Your code:
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
The correct code, using the variables you already declared above:
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name, $db_port);
More info can be found here: http://www.php.net/manual/en/mysqli.quickstart.connections.php
For a complete example, it will look like this.
$env = json_decode(file_get_contents("/home/dotcloud/environment.json"));
$db_user = $env->DOTCLOUD_DB_MYSQL_LOGIN;
$db_password = $env->DOTCLOUD_DB_MYSQL_PASSWORD;
$db_host = $env->DOTCLOUD_DB_MYSQL_HOST;
$db_port = $env->DOTCLOUD_DB_MYSQL_PORT;
//I also define $db_name here
$db_name = 'mydbname';
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name, $db_port);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
try like this: I think it will help you.
$connect = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
//echo 'Connected successfully';
$db = mysql_select_db($mysql_database,$connect);
if (!$db) echo"'Could not select database";
Related
I have a question that is really bothering my understanding of working with php and MySQL. The problem is when i try to connect to my online DB.
If I Write my code like this:
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASS', 'pass!');
define('DB_HOST', 'localhost');
I connect with no problems. Great, that works!
But, when i want to connect like this:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "name";
$conn = new mysqli($servername, $username, $password, $dbname);
I get this error: "Access denied for user..."
Can somebody please help me with this silly problem?
This code works for both options
<?php
//Creates static credentials
define('DB_NAME', 'data');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
//Creates connection to the database
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
//Checks for connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//If there are no connection, error
if (!$con) {
die('Could not connect' . mysqli_error());
}
//Select the 'data' database
$con->select_db(DB_NAME);
//Checks if database 'data' has been selected
if (mysqli_select_db($con, DB_NAME)) {
echo "Database exists <br>";
} else {
echo "Database does not exist";
}
if($con){
echo "connection succss";
}
?>
option 2
<?php
$servername="localhost";
$username ="root";
$password="";
$dbname="test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "connected";
}
?>
Those are my codes, and also yours is working fine, might be a problem, with your username/password
Try this:
$mysql_host = 'localhost';
$mysql_user = 'user';
$mysql_password = 'pass';
$mysql_database = 'localhost';
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
#mysql_select_db($mysql_database);
Also check the privelges for that user.
This happens some time because you have changed user name and password of data base or you can try make your user as root and password empty.
I am fresher for Open shift.
Here is my problem in Open shift.
I completed creating application with php, Mysql and PhpMyAdmin
Then I went to phpMyAdmin and created a new database 'Students_list'
Now I am unable to connect to my database with the following code in php pages.
$db_host = $_ENV['127.12.123.2']; //sample host
$db_user = $_ENV['adminnxUpXA6'];
$db_pass = $_ENV['lyPfbtHYpDFcU'];
$db_name = $_ENV['students_list']; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
May I please know where I am comitting the error.
Try this instead:
$db_host = getenv('OPENSHIFT_MYSQL_DB_HOST'); //sample host
$db_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$db_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$db_name = 'students_list'; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
You are using $_ENV, which contains environment variables from the operating system. Most probably what you wanted to do is this:
$db_host = 'ip';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'students_list';
The structure for the mysqli connection string is this:
mysqli(Hostname,Username,Password,DatabaseName);
I'm trying to learn this stuff. Please be gentle.
Something is wrong here:
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysql_select_db($dbname . $dbconnected) or die ($dberror1);
where is my mistake? I want $dbconnected to show...
I can just as easily use
echo "hello";
it shows that I connect but I'm trying to get familiar with using multiple variables.
would this be better?
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname))
echo $dbconnected;
else die($dberror1);
Right now you are trying to connect to a database called something_dbnameyou are connected. The . concatenates variables into one string.
To fix your immediate problem, try this:
First, define $dbhost - I don't see it in your code.
Then change the last line to this:
$select_db = mysql_select_db($dbname) or die ($dberror1);
Then, just echo $dbconnected;
If you are not connected, the page will have called die, and will never reach the line that echos $dbconnected. If you are connected, the program will proceed to this next line and echo your success message.
Or you can do it more explicitly like this:
if ($select_db = mysql_select_db($dbname))
echo $dbconnected;
else die($dberror1);
To fix the bigger problem, DON'T use mysql_*. Read this for more information.
mysqli or pdo are far better options, and you can accomplish the same task easier, for instance, connecting to a db with mysqli is just:
$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
Or you can do it procedural style, which is closer to your current code. The following snippet is from the php manual, on the page I linked in the comment below.
$link = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
I'd strongly recommend using PDO. The connection string is similar and can be done using:
// I do not see $dbhost defined in your code. Make sure you have it defined first
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $dbconnected; // will print out the connection success message
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
To answer your question about not using mysql_* functions, you can check out this
I have the following sqlconnection.php
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
if ($mysqli->connect_error()) {
die('Connect Error (' . $mysqli->connect_rror() . ') '
. mysqli_connect_error());
}
with myuser, mypassword and mydb values being set correctly.
Anyway, although I am able to do operations with phpmyadmin (I got that installed aswell) I can't get to connect to the database. Any idea on why and what exactly I should put in localhost?
Just try this way~
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'password';
$dbname = 'dbname';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);
if all your db information is correct, should be working
If I use the following code, it works:
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
But when I do this, it doesn't:
$db_host='localhost';
$db_id='root';
$db_pass='';
$con = mysql_connect($db_host, $db_id, $db_pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
Trying to swap (") and (').
mysql_ functions are discouraged for new applicationa you are advised to use mysqli or PDO. The following code uses PDO to connect to database.
//dependant on your setup
$host= "localhost";
$username="XXX";
$password="YYY";
$database="ZZZ";
// connect to the database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
//Remainder of code
}
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);//Change file name to suit
}
// close the connection
$dbh = null;
try using a script like this
$db_host = 'localhost';
$db_id = 'root';
$db_pass ='';
$con = mysql_connect ($ db_host, $ db_id, $db_pass) or die ('Could not connect:'. mysql_error ());
That code is fine. Review the error log- the problem has to be external to that code.