Whenever I try to connect to my database with my credentials, it doesn't connect with the username, for example when I try to connect to my local database with
mysqli_connect("localhost","dbuser","","database")
It returns
Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'database'
You can't connect using all four parameters on mysqli_connect()
From the above code i can tell your
host : "localhost"
user: "dbuser"
password: ""
database name: "database"
Do this,
$conn = mysqli_connect("localhost", "dbuser", "");
mysqli_select_db($conn, "database");
// to check if you're connected, after tested and see you can take off the code below...
if ($conn) {
echo "Connected";
} else {
echo "Connection failed";
}
if you seriously want to connect using all four parameter above?
$conn = new mysqli("localhost","dbuser","","database");
// to check if you're connected, after tested and see you can take off the code below...
if ($conn) {
echo "Connected";
} else {
echo "Failed";
}
Hope this was helpful?
Just put a space between the quotes like this " " instead of "" to show as blank in the password value in your query because in programming a value either 0(false) or 1(true) has to be there you cannot have nothing and expect something
This is when you have not set password for the user in your local phpmyadmin like root user and also check for spelling mistakes
Related
I have a mysql database running on my local machine and I use wamp. I can login with phpmyadmin. Everything looks fine, create tables and users.
When I try to connect with php I get the message connection refused. The same when I try 127.0.0.1/connect.php in my browser. If I have a wrong user in my php file I get the message that user ist wrong.
Here my connect.php
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "users");
if ($conn) {
die('no connection: '. mysqli_connect_error());
}
echo 'connected';
mysqli_close($conn);
?
I tried everything, localhost, IP, 10.0.2.2, but nothing works. I also checked mysql settings and I read most of the posts here.
I solved the problem.
With this code it works perfect
<?php
$conn = new mysqli("127.0.0.1", "root", "", "users");
if (mysqli_connect_errno()) {
echo "no connection " . mysqli_connect_error();
}
echo 'conneted';
mysqli_close($conn);
?>
So, I'm SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.
I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.
Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'servername' (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user 'username'#'servername' (using password: YES)
To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can't login with PHP?
Here's my code (taken pretty much directly from W3Schools):
<?php
$servername = "servername";
$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";
?>
(Hopefully it's obvious that I've changed my servername/username/password for the purposes of this post)
Check the details correctly, By deafult
host = localhost
username = root
password = (No password leave it as empty)
database = (your database)
<?php
$connection = new mysqli('host','username','password','your_database');
if($connection->connect_error || $connection->error){
echo "error";
}else{
echo "done";
}
?>
you should add database name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo"
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
//if you are using xampp you shoul locate this file you get information from this
passwords.txt
MySQL database username/password is not necessarily same as cPanel username & password.
Here is what you should do:
Login to your cPanel
From dashboard click "MySQL® Databases"
Add a new user from there (https://prnt.sc/kpiby9). Just fill username
and password (note down the password).
Add that user with your database by selecting both from the dropdown
(https://prnt.sc/kpidqx)
Now, use this username & password to connect with the database.
mysqli_connect(serverhostname,username,password,dbname);
I am quite new to the PHP world. I have installed MAMP on my Mac. This is the code I have written to connect to MySQL:
<?php
// Connect to the database server
$dbcnx = #mysql_connect("localhost", "root", “root”);
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>
MySQL has already started and is running. I am not sure why am I not able to connect.
Try this
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "");
if (!$dbcnx) {
echo( "Cannot connect to database due to ". mysql_error());
exit();
}
?>
Since you are getting Access denied for user 'root'#'localhost' (using password: YES) there is no need of giving password to connect with the database.So try with password ''.
Note:- mysql_* functions are deprecated as of PHP 5.5.0,and will be removed in the future.Instead, the MySQLi or PDO_MySQL extension should be used
Your code has smart quotes around the password. Try replacing them with regular quotes. If you have not changed the default MAMP password, this should work:
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "root");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>
so I'm making a super simple site just for practice where you type something in an then its displayed on the page, but it wont work and i think iv narrowed it down to the begining where it connects to the database just
$server ="localhost";
$username ="root";
$password ="********";
$database ="user_accounts";
$connect=mysqli_connect($server,$username,$password,$database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Im running my server using Apache/2.2.22 (Debian), PHP 5.4.4-14 and mysql Ver 14.14 Distrib 5.5.31, for debian-linux-gnu (armv7l) using readline 6.2
and I've noticed that even when I type the password incorrectly it doesn't 'echo' the error line, so my question is do I connect to localhost like that or did i do something else wrong in there?
Try something like this:
if ($mysqli->connect_errno) {
printf("Failed to connect: %s\n", $mysqli->connect_error);
exit();
}
Could you Try this
<?php
$mysqli = #new mysqli('localhost', 'root', '*******', 'user_accounts');
if ($mysqli->connect_errno) {
die('Connection Error: ' . $mysqli->connect_errno);
}
?>
Connect like below
<?php
$link = #mysqli_connect('localhost', 'root', '*******', 'user_accounts');
if (!$link) {
die('Connect Error: ' . mysqli_connect_errno());
}
?>
Below is create user in mysql script on your request
User newuser to connect with no password (which is insecure), include no IDENTIFIED BY clause:
CREATE USER 'newuser'#'localhost';
To assign a password to new created user newuser, use IDENTIFIED BY with the plaintext password value:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'mypass';
To avoid specifying the plaintext password if you know its hash value (the value that PASSWORD() would return for the password), specify the hash value preceded by the keyword PASSWORD:
CREATE USER 'newuser'#'localhost'
IDENTIFIED BY PASSWORD '*90E462C37378CED12064BB3388827D2BA3A9B689';
Then you can GRANT access of what ever you want to that newuser
GRANT SELECT,INSERT,UPDATE,DELETE ON user_accounts.* TO 'newuser'#'localhost';
You can use this link to generate the hash code.
I have 2 connection database (MySQL) in different host like this:
//database 1
$dbhost='localhost';
$dbuser='user1';
$dbpass='pass1';
$dbname='dbname1';
//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';
function dbconnection($dbhost,$dbuser,$dbpass,$dbname){
if(!mysql_connect($dbhost,$dbuser,$dbpass)){
echo "Error occure on opening connection or database. Period.";
}else{
if(!mysql_select_db($dbname)){
echo "Error occure on select databases !";
}
}
}
function dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams){
$cxn = mysql_connect($dbhostams,$dbuserams,$dbpassams,$dbnameams);
if( $cxn === FALSE ) {
die('mysql connection error: '.mysql_error());
}else{
if( !mysql_select_db($dbnameams) ){
echo "Error occure on select databases !";
}
}
}
when i use:
dbconnection($dbhost,$dbuser,$dbpass,$dbname);
at my page code, and use:
dbconnectionams($dbhostams,$dbuserams,$dbpassams,$dbnameams);
at another line of code in same page, error occured, like this:
Warning: Access denied for user: 'apache#localhost' (Using password: NO) in
/home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php on line 17
Warning: MySQL Connection Failed: Access denied for user: 'apache#localhost'
(Using password: NO) in /home/firman/html/fdrsimpeg/sdm-aam/include/dbclass.php
on line 17
mysql connection error:
what must i do to solve this problem?
Your global variables ($dbhost, $dbuser, etc.) are not within the scope of your function calls; for example, if those function calls take place within another function, you will need to declare the variables within the function with the global keyword in order to access them (otherwise PHP thinks you're referring to different variables of the same name that are local to the function):
function foo() {
global $dbhost, $dbuser, $dbpass, $dbname;
dbconnection($dbhost,$dbuser,$dbpass,$dbname);
}
Read more on PHP variable scope.
First of all, change the variable names, it will be easier for you to read the code ... Second, the problem seems to be with the connection, as it is detecting that the username is "apache". Check the variables names in the functions ... change them and try again.
//database 1
$dbhost='localhost';
$dbuser='user1';
$dbpass='pass1';
$dbname='dbname1';
//database 2
$dbhostams='192.168.16.3';
$dbuserams='user2';
$dbpassams='pass2';
$dbnameams='dbname2';
function dbconnection($_dbhost,$_dbuser,$_dbpass,$_dbname){
if(!mysql_connect($_dbhost,$_dbuser,$_dbpass)){
echo "Error occure on opening connection or database. Period.";
}else{
if(!mysql_select_db($_dbname)){
echo "Error occure on select databases !";
}
}
}
function dbconnectionams($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams){
$cxn = mysql_connect($_dbhostams,$_dbuserams,$_dbpassams,$_dbnameams);
if( $cxn === FALSE ) {
die('mysql connection error: '.mysql_error());
}else{
if( !mysql_select_db($dbnameams) ){
echo "Error occure on select databases !";
}
}
}
You need to pass true as fourth parameter to the mysql_connect function.
$database1 = mysql_connect($host1, $user1, $pass1);
$database2 = mysql_connect($host2, $user2, $pass2, true);
mysql_select_db('database1', $database1);
mysql_select_db('database2', $database2);
And to query your database use this:
mysql_query('SELECT * FROM table', $database1);
mysql_query('SELECT * FROM table', $database2);
Check this answer here on Stackoverflow....link
I didn't see anything obviously wrong, I tested your code as provided and both connections worked.
Check to see if mysql.default_user, mysql.default_host etc. is set within your php.ini