Warning: mysqli_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'dbtest' [closed]

ε祈祈猫儿з 提交于 2020-05-17 08:33:33

问题


When i try to connect to my sql server, it gives me the error:

Warning: mysqli_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'dbtest'.

I just created the user and gave it all permissions, but i still dosent work. Before i used another account, but i couldn't change the database perssions for it. Anybody who have a potential fix?

Here is the connect code.

<?php

    // this will avoid mysql_connect() deprecation error.
    error_reporting( ~E_DEPRECATED & ~E_NOTICE);
    // but I strongly suggest you to use PDO or MySQLi.

    $DBHOST= "localhost";
    $DBUSER= "testadmin";
    $DBPASS= "";
    $DBNAME= 'dbtest';

    $conn = mysqli_connect(localhost, testadmin, $DBPASS, dbtest);
//  $dbcon = mysql_select_db($DBNAME);

    if ( !$conn ) {
        die("Connection failed : " . mysql_error());
    }

?>

回答1:


I know this thread is old, but Ahmad Sharif is correct. The best way to quickly resolve this issue, especially if you're with a new hosting company, is to just create a new database using the "MySQL Database Wizard" in cPanel, then grant ALL privileges to everyone to ensure the remote access will work. Just make sure that ALL check boxes are checked while creating the new database from the "MySQL Database Wizard".




回答2:


Please check that you check all grant privileges. If you do not then delete the previous DB and create a new Db from Mysql Database Wizard (I guess it is shared hosting). In the last step, you can check all the privileges.

I have got the same error and I could solve my problem by grant all privileges.




回答3:


In my case grant all privileges to 'my_user' solved the problem

GRANT ALL PRIVILEGES ON *.* TO 'my_user'@'%' ;

You can check the following link for further information https://mariadb.com/kb/en/grant/




回答4:


There is no host, no user and no db-name in your mysqli_connect call. Do:

$conn = mysqli_connect($DBHOST, $DBUSER, $DBPASS, $DBNAME);

or

$conn = mysqli_connect('localhost', 'testadmin', $DBPASS, 'dbtest');

And if you wouldn't suppress E_NOTICEs, you'd see that php is looking for undefined constants.




回答5:


I don't know if anyone had a solution to this I have just been trying to create a separate table for a list of emails with a same key column. I got exactly the same error message but solved it by writing a php file which i can run on the server.... The php file creates the table ! And it had permissions to let me write to it as well !

Here is the file i used to let the server make the table i needed. And let me access it from my other php files. I will call it here "LetServerMakeTable.php" ha! When i uploaded it to my 'ricky' heliohost server i ran it on the browser thus: http://virowiz.heliohost.org/LetServerMakeTable.php

Result:... I had a php file which collecter email addresses and a used ID code and the wrote it successfully to my new Emails table within my database.

<html>
<head>

<?php
$conn = mysqli_connect("localhost", "virowiz_Kevin", "password", "virowiz_Covid3a");

// Check connection
if (!$conn)         //<---- ! conn = NOT conn
    {
    echo "Failed";
        die("Connection failed: " . mysqli_connect_error());
    }
else
    {
    echo "Connected successfully";
    }


//---------------------------------- Create the table.
$sql = "CREATE TABLE Emails
(
CVTRn BIGINT(12),
Email char(254),
Mobile char(13)
)";
//----------------------------------


mysqli_error($conn);

if (mysqli_query($conn, $sql))
    {
        echo "Table created successfully";
    }
else
    {
        echo "Error creating table: " . mysqli_error($conn);
    }


mysqli_close($conn);

?>

</head>


</body>
</html>


来源:https://stackoverflow.com/questions/47247595/warning-mysqli-connect-hy000-1044-access-denied-for-user-localhost-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!