I'm still very new in uploading php and mysql online. Here is the link on where I uploaded my php files http://studcams-shadowj3yz.rhcloud.com/main.php
It contains a config.php and connection.php and here are the codes
For config.php
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'));
For connection.php
$this->connect = mysqli_connect(constant("DB_HOST"), constant("DB_USER"), constant("DB_PASS"), constant("DB_NAME"), constant("DB_PORT"))
or die("Could not connect to db");
This is how I connnect to openshift right? It displays Could not connect to db. How do I fix the this? Thanks!
Edited:
Ok now I've changed my code into this
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'));
$dbhost = constant("DB_HOST");
$dbport = constant("DB_PORT");
$dbusername = constant("DB_USER");
$dbpassword = constant("DB_PASS");
$db_name = constant("DB_NAME");
$this->connect = mysqli_connect($dbhost, $dbusername, $dbpassword, "", $dbport) or die ("Connect Error: ".mysqli_error($this->connect));
mysqli_select_db($this->connect, $db_name) or die("DB Error: ".mysqli_error($this->connect));
Now it displays Connect Error: but no other information.
Related
I recently installed php7.4-fpm (FPM/FastCGI). But I'm not able to connect to MySQL database.
I'm using the following configuration. Note my database has no password. Tried 'localhost', '127.0.0.1', 'http://127.0.0.1:3306', '127.0.0.1:3306', in DB_SERVER setting but no luck.
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '127.0.0.1');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'republic');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("Oops! Something went wrong. Please try again later.");
}
?>
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.
in dbconect
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo " can't connect : " . mysqli_connect_error();
exit;
}
?>
and in dbconfig
<?php
define('DB_HOST', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_NAME', "carwash");
define('DB_PREFIX', "carwash_");
?>
and output
Warning: mysqli_connect() [function.mysqli-connect]: (42000/1049): Unknown database 'carwash' in C:\xampp\htdocs\carwash\includes\dbconnect.php on line 2
can't connect : Unknown database 'carwash'
I don't know what's wrong or what's happened, it happen only today but the day pass it didn't happen
Make sure you have a database named carwash.
Xampp comes with phpmyadmin,have a look for the databases you have there.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
This is my dbcon file in PHP.
Basically, I need to connect my PHP application with openshift MySQL DB. Here's what I did.
<?php
// Database Connection Setting
$dbhost = "127.0.0.1"; // Host name
$dbport = "3308"; // Host port
$dbusername = "user"; // Mysql username
$dbpassword = "pass"; // Mysql password
$db_name = "mf"; // Database name
$mysqlCon = mysqli_connect($dbhost, $dbusername, $dbpassword, "", $dbport) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, $db_name) or die("Error: " . mysqli_error($mysqlCon));
?>
This gives me an error on openshift but works for other PHP apps. I get nothing on error explanation only as Error: { ...blank space... }.
I've made it to work by doing this.
Global Use
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'));
$dbhost = constant("DB_HOST"); // Host name
$dbport = constant("DB_PORT"); // Host port
$dbusername = constant("DB_USER"); // MySQL username
$dbpassword = constant("DB_PASS"); // MySQL password
$db_name = constant("DB_NAME"); // Database name
Alternatively
$dbhost = getenv('OPENSHIFT_MYSQL_DB_HOST'); // Host name
$dbport = getenv('OPENSHIFT_MYSQL_DB_PORT'); // Host port
$dbusername = getenv('OPENSHIFT_MYSQL_DB_USERNAME'); // MySQL username
$dbpassword = getenv('OPENSHIFT_MYSQL_DB_PASSWORD'); // MySQL password
$db_name = getenv('OPENSHIFT_GEAR_NAME'); // Database name
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_APP_NAME'));
$mysqlCon = mysqli_connect(DB_HOST, DB_USER, DB_PASS, "", DB_PORT) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, DB_NAME) or die("Error: " . mysqli_error($mysqlCon));
Or better still, just use the environment variables in your connection string:
$mysqlCon = mysqli_connect(getenv('OPENSHIFT_MYSQL_DB_HOST'), getenv('OPENSHIFT_MYSQL_DB_USERNAME'), getenv('OPENSHIFT_MYSQL_DB_PASSWORD'), "", getenv('OPENSHIFT_MYSQL_DB_PORT')) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, getenv('OPENSHIFT_APP_NAME')) or die("Error: " . mysqli_error($mysqlCon));
I have a question that need all of you to help me. I got stuck with it several days. My problem is related to the connection to database using php. Both Mysqli and Mysql are enabled in phpmyadmin. I can connect to database through Mysqli, but i cannot connect through Mysql.
Here is code
Mysqli
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
define('PORT', '80');
define('SOCKET', '/var/lib/mysql/mysql.sock');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE, PORT, SOCKET) or die("Error " . mysqli_error($connection));
mysqli_set_charset($connection, "utf8");
?>
Mysql
<?php
define('DB_SERVER', 'localhost:/var/lib/mysql/mysql.sock');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die("Error " . mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset($connection, "utf8");
?>
Can you help me to solve this problem?
Thank in advance!
which type of error you see???
try to use instead of
define('DB_SERVER', 'localhost');
define('DB_SERVER', 'ipOfTheHost');
// data db
$username = "username";
$pass = "pwd";
$database = "nameDB";
$host = "ipServer"; //my is 62.149.150.187
// connection
mysql_connect($host, $username, $pass);
#mysql_select_db($database) or die("Argh... it's impossible to connect to it");