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);
Related
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
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.
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
I am having issues connecting from a LAMP over to an DB2 iseries. Any thoughts on this code? I cannot get it to connect. How can I confirm that the iseries driver works, is installed, etc. Or is my code wrong? Thanks in advance...
$db_name = "mydatabase";
$db_host = "ip address";
$db_user = "user";
$db_pass = "mypass";
$dsn = "DRIVER={iSeries Access ODBC Driver};" .
"CommLinks=tcpip(Host=$db_host);" .
"DatabaseName=$db_name;" .
"uid=$db_user; pwd=$db_pass";
$odbc = odbc_connect($dsn, $db_user, $db_pass);
if (!$db = odbc_connect ($dsn, $db_user, $db_pass)) echo 'Error!';
else echo 'Success!';
$res400 = odbc_columns($odbc, "mylibrary", "%", "myfile", "%") or die(odbc_errormsg());
echo odbc_result_all($res400);
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";