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");
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 seen some ways to connect to the MySQL DB with PHP, but don't know why the one is better than the other, or safer or anything. Same thing goes with inserting or fetching data to/from the database.
This is some examples of MYSQL connection with PHP:
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$db = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to database. '.mysqli_connect_error());
OR
class kobling extends mysqli {
public function __construct(
$host = "127.0.0.1",
$base = "test",
$user = "root",
$pw = "password",
$port = "3306") {
parent::__construct($host,$user,$pw,$base,$port);
}
}
if (!($db = new kobling())) {
die("Kunne ikke koble til databasen.");
}
Any suggestions or solutions?
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'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.
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.