0

So I have this site. And I want to edit a div based on some database data.

I found out that a fairly easy solution would be to connect with mysqli("servername", "username", "password", "database").

Problem is, I can't connect to the database

I think it has to do with what I should input to the servername username and password fields.

I have tried for the servername:

  • localhost
  • damon.multiserver.gr (which is the hostname of the myphp database)
  • I also performed nslookup to damon.multiserver.gr and used that ip
  • the site's url

for the username:

  • root
  • created new user with desired permissions

for the password:

  • ""
  • the password from the user I created

The database elements are in multiple languages, including Greek.

$mysqli = new mysqli("localhost:3306", "root", "", "grcrenta_2020");
if($mysqli->connect_error) {
    exit('Could not connect');
}
...more code

Thanks in advance for your help :)

  • 3
    Have you tried outputting `$mysqli->connect_error` to see what it actually says? It's easier to debug if you check the real error message instead of hiding it behind a custom one. – M. Eriksson Nov 27 '21 at 12:50
  • It says "Access denied for user 'root'@'localhost' (using password: NO)" – NIKOLAOS ILIOPOULOS Nov 27 '21 at 13:00
  • 4
    There are multiple possible problems. 1) For your localhost connection: root login is disabled on MySQL server 2) On your remote DB: The user was created using 'user'@'localhost'. This restricts the user to only be able to connect from the same server. Use 'user'@'%' to enable remote access 3) The port on the remote server isn't opened. – stui Nov 27 '21 at 13:13
  • Have you tried using the actual root password, the one you created when you installed mysql – ADyson Nov 27 '21 at 13:15
  • 1
    @stui - lol, yes , but I can certify the port is open on that host. – YvesLeBorg Nov 27 '21 at 13:16
  • 1
    read all about granting privileges to a user : many places you can find this, like [here](https://linuxhint.com/create-new-user-mysql/). Also, dunno what that host is, but opening 3306 on the internets of hacks is a bad idea. – YvesLeBorg Nov 27 '21 at 13:19
  • See this answer https://stackoverflow.com/a/60668665/12232340 –  Nov 27 '21 at 13:20
  • @NIKOLAOSILIOPOULOS which mysql version you are using? – SM Ataul Karim Riad Nov 27 '21 at 13:28
  • @NIKOLAOSILIOPOULOS Please [edit] your question to include a description how exactly you have created the users to your database. There might be a simple issue with `FLUSH PRIVILIGES`. Also include the result of `SELECT Host, User FROM mysql.user`. Also describe how you can connect to your database outside of your PHP script. – Progman Nov 27 '21 at 13:39

1 Answers1

-1

You can try this:

$mysqli -> new mysqli(host, username, password, dbname, port, socket);

$mysqli = new mysqli("localhost", "root", "", "grcrenta_2020",3306);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
abdux
  • 1