-2

I define:

$hostname= "localhost";
$database = "company";
$username = "dxy";
$password= "abc";
$c = mysqli_connect($hostname, $username, $password);

I get below error:

Error 2: Access denied for user 'root'@'localhost' (using password: NO)

Why it tries to connect via root@localhost rather than dxy@localhost?

I have another application on this dedicated server. But it connects via username@localhost not root....what is the wrong with this?

When I change mysqli to mysql, it connects perfectly.

I appreciate your help.

Michael zand
  • 15
  • 2
  • 9
  • I can't see this code generating that error message. Are you sure it's not coming from somewhere else in the code, or that your variables aren't being overwritten? – aynber Sep 17 '18 at 20:03
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Sep 17 '18 at 20:06
  • I added 4th parameter, no luck. When I change mysqli to mysql, everything works perfect. No, it's a connection error. I am using Plesk for managing my server, so not sure if it has to do with this – Michael zand Sep 17 '18 at 20:06
  • I'll move to mysqli API in near future...my application is heavy with over 200 dynamic pages... – Michael zand Sep 17 '18 at 20:08
  • 2
    There isn't enough code to support the question. I have a feeling that you're not showing/telling us everything. I think you're still using some of the old mysql_* functions and probably one doesn't have a db connection for it. I'm next to 100% sure that you're mixing different mysql apis. – Funk Forty Niner Sep 17 '18 at 20:46

1 Answers1

1

First you left out the database in your connection so it doesn't know where to connect. Second create a new mysqli() function to define a db connection. Hope this helps.

$hostname  = 'localhost';
$username  = 'dxy';
$password  = 'abc';
$database  = 'company';
$c =  new mysqli($hostname, $username, $password, $database);
Jonny
  • 1,303
  • 1
  • 10
  • 20
  • 1
    The database argument isn't needed to just connect. It is however required when querying a table though. I have a feeling that this goes further/deeper than they've let us known. – Funk Forty Niner Sep 17 '18 at 20:46
  • 1
    As one of your guys suspected, the issue with application itself not db conn. The actual error was: Error:2 mysql_real_escape_string(): Access denied for user 'root'@'localhost' (using password: NO)....so I changed mysql_real_escape_string() to mysqli_real_escape_string() && added first conn parameters...Now everything works great. Lesson learned not to use mixed MySQL old and new MySQLi together... – Michael zand Sep 17 '18 at 21:16