php/mysql connection error(with config file) - php

I am developing a login/register system for a website but when i'm trying to connect to the database it gives this error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\register.php on line 17
I'm using a external config file wich is been imported.
My config file:
// Connection details
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
My connection code:
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
I found one solution that worked to connect but then my config file was useless.
No config but only this:
// Create connection
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
If someone nows a solution where i can keep using my config file please post it here.
Thanks in advance.

Well, if the connection works with this values:
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
But not with this values:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "cloud";
Why don't you just set the "working" values into your config file. So it would look like this:
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
Another solution which is described here includes the creation of a new user, so you don't connect via root, but the new created user.

Related

Accessing a MySQL database with php file

I have a MySQL database and I am trying to figure out how to access it with a php file. I keep getting an error, so I was wondering if that was because my hostname is incorrect. If so, could anyone help me identify exactly what it is? The database is hosted on this website: https://christopherliao2002.000webhostapp.com/.
Below is my HTML code.
<?php
//Step1
$db = mysqli_connect('localhost','***********','password','******')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
<?php
//Variables
$servername = "localhost";
$username = "*****";
$password = "****";
$dbname = "******";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
</body>
</html>
Try this format
$servername = "localhost"; // This is fixed for 000webhost.
$username = "id***_name";
$password = "****";
$dbname = "id***_name";
Try this without the try / catch:
//Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
Reference: https://www.w3schools.com/php/php_mysql_connect.asp
Here the problem is with your hostname. Normally the hostname will be 'localhost' and in 000webhost the hostname is something like mysql#.000webhost.com and '#' needs to be replaced with the number for that you need to check your hosting providers members area.
And please don't post your username and password publicly
$db = mysqli_connect('mysql#.000webhost.com','xxxxx','password','xxxxxx')
or die('Error connecting to MySQL server.');
?>

How to connect MYSQL with PHP

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

Cannot change php from localhost details to my servers to connect sql database

I am new to php/sql and i have a login system which runs on my local host using xammp and it all works fine. I now want to upload it to my website but the code no longer works... I have created a sql db on my hosting service and tried to change the code.
the code that is used on the local host is
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is the code that i have got from my hosting.
<?php
$host_name = 'db682827654.db.1and1.com';
$database = 'db682827654';
$user_name = 'dbo682827654';
$password = '<Enter your password here.>';
$conn = mysql_connect($host_name, $user_name, $password, $database);
if (mysql_errno()) {
die('<p>Failed to connect to MySQL: '.mysql_error().'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p >';
}
?>
however this brings up an errror message. I have changed the password to the password for my database but its still not connecting.
This is the error message.
Failed to connect to MySQL: Access denied for user 'dbo706265806'#'217.160.62.78' (using password: YES)
Any help would be greatly appreciated
use mysqli_connect (not mysql_connect) like localhost:
<?php
$dbServername = "db682827654.db.1and1.com";
$dbUsername = "dbo682827654";
$dbPassword = "<Enter your password here.=)>";
$dbName = "db682827654";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
On your computer you are using mysqli_connect, but on the server you are trying to use mysql_connect. Just use the same file from your computer and simply change $dbServername, $dbUsername, $dbPassword and $dbName to match those that your hosting provided.

Connection error MySQL

I copied this from W3Schools and when I try to run it it gives me the a error. I am really new to MySQL so I am trying to fix this but I don't know how.
ERROR:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user
'username'#'localhost' (using password: YES) in
C:\xampp\htdocs\Informatica\test.php on line 10 Connection failed:
Access denied for user 'username'#'localhost' (using password: YES)
<html>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
</body>
</html>
your username or password is incorrect if you are very new and just starting with the local server try
$username = "root";
$password = "";
as your username and password
W3schools don't know your localhost details, do the following
$servername = "localhost";
$username = "root"; //put your username here
$password = ""; // your password here
Leave password empty and put "root" in username, that's the default xampp setting.
you have to add correct information provided by your hosting provider here :
$servername = "localhost";
$username = "username";
$password = "password";
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
//Connection to mysql check
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($conn,'databaseName');
?>

c9 access a mysql database from another project

I have one project running a database on a Cloud9 project. I have created a mobile app which will need to connect to the database by POST to a PHP file which will then run the following code:
$servername = "myusername-projectname-somenumber";
$port = 3306;
$username = "form";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, 3306);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I got the servername by using this in the terminal:
SHOW VARIABLES WHERE Variable_name = 'hostname';
I am getting an error from the other project:
Connection failed: Unknown MySQL server host 'myusername-projectname-somenumber' (0)
***Note: I have replaced myusername-projectname-somenumber and dbname for posting this. I did not forget to fill them out.
Thanks for the help!

Categories