0

I'm getting this error Access denied for user 'user'@'localhost' (using password: NO) when I open a second mysqli connection in the same script.

I have a class which has 2 properties(Subclasses). This both properties extends from the DataBase class(which makes a new mysqli connection). At the moment when this subclases get instantiate I got that Access denied error.

Is there a way to establish two mysqli connection or my logic is wrong and a class shoulnd't have 2 subclasses like this ?

I hope I could explain myself. Thanks in advance

Mollo
  • 723
  • 3
  • 7
  • 24
  • 2
    if i may ask, Why do you open 2 mysqli connections? why not just use the same? – Jesper Kaae Nov 05 '14 at 19:16
  • @thecakei Because both subclasses extends from the DataBase class. In this case I need to use both in the same script but there are other scripts where they have to work independently – Mollo Nov 05 '14 at 19:20

2 Answers2

1

There's no reason I can think of to open 2 connections to the same database server. You should create your connection and pass it to your functions as an argument.

$mysqli = new mysqli('localhost', 'username', 'password', 'database');

class Foo {
      /** @var \mysqli */
      protected $db;

      public function __construct(mysqli $mysqli) {
           $this->db = $mysqli;
      }

      public function bar() {
           $res = $this->db->query('SELECT * FROM table');
           $data = array();
           while($row = $res->fetch_assoc()) $data[] = $row;
           return $data;
      }
}

$class = new Foo($mysqli);
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • This could work, but I will have to pass the connection every time that I instantiate a class. – Mollo Nov 05 '14 at 19:36
  • You could create a [Singleton](http://stackoverflow.com/q/8776788/2370483) if it's that big of a deal – Machavity Nov 05 '14 at 19:46
  • I kinda liked your solution but with a few changes, make that $mysqli global variable and just use it directly through the Database class – Mollo Nov 05 '14 at 19:58
0

Check your max_user_connections on your my.cfg file.

  • max_user_connections

    The maximum number of simultaneous connections permitted to any given MySQL user account. A value of 0 (the default) means “no limit.”

    This variable has a global value that can be set at server startup or runtime. It also has a read-only session value that indicates the effective simultaneous-connection limit that applies to the account associated with the current session. The session value is initialized as follows:

    If the user account has a nonzero MAX_USER_CONNECTIONS resource limit, the session max_user_connections value is set to that limit.

    Otherwise, the session max_user_connections value is set to the global value.

jherran
  • 3,337
  • 8
  • 37
  • 54
  • What if in my my.cfg file I don't event have max_user_connections variable defined/written ? – Mollo Nov 05 '14 at 19:29
  • 1
    This is no problem. By default is zero, that means no limit. – jherran Nov 05 '14 at 19:30
  • Alright, so thats not the problem hehe ... Thanks for your time – Mollo Nov 05 '14 at 19:33
  • The `max_user_connections` can be a user permission too (so not necessarily in the my.cfg). The following command should show whether or not it is set specifically for the user: `SHOW GRANTS FOR 'user'@'localhost';` – cOle2 Nov 05 '14 at 19:40