1

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());
    }

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • _Note:_ You're mixing API's. `mysqli_*` and `mysql_*` are _not_ the same or interchangeable. You should also wrap strings in quotes (`'localhost'` instead of `localhost` etc). – M. Eriksson Nov 12 '17 at 10:19
  • _"this will avoid mysql_connect() deprecation error"_ **1.** You're not using `mysql_connect()`. **2.** Instead of hiding problems, solve them. Suppressing errors is a really bad practice, making it much harder for you to debug your own code. – M. Eriksson Nov 12 '17 at 10:24
  • Possible duplicate of [mysql\_connect(): No connection could be made because the target machine actively refused it](https://stackoverflow.com/questions/21987746/mysql-connect-no-connection-could-be-made-because-the-target-machine-actively) – Progman Nov 12 '17 at 11:49

5 Answers5

4

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".

Aaron Esteban
  • 71
  • 2
  • 8
2

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/

Salvador Rueda
  • 855
  • 2
  • 7
  • 15
0

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.

simon.ro
  • 2,984
  • 2
  • 22
  • 36
0

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.

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
-2

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>
Kevin
  • 1
  • 1
  • Hello Myself... I have made a mistake: The HY000/1044 arose for me because in phpmyadmin I created the first file with the MySql Databases facility - which gave me a page of permissions to modify (i accepted them all) and the 2nd unsuccessful time with the MySQL database Wizard. Which at that time did not give me a permissions page to alter or accept. The table created resulted in php files returning a permissions error. With me the jury is still out as to whether this is the reason but one way works and the other doesnt. Kevin. – Kevin Jul 19 '20 at 16:01