0

Trying to connect to a db with

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} else {
  echo '.';
}

and I get

Connection failed: Access denied for user '*'@'localhost' to database '*'

but if I use

$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} else {
  echo '.';
}

it works! What gives?

I've used new mysqli before in and it worked fine. I know my username/password are right. Its GoDaddy and I clicked all of the permissions. What is going on?

Edit:

Running this:

<?php 
ini_set('display_errors',1);
error_reporting(E_ALL);

$servername = "localhost";
$username = "testuser";
$password = "testpassword";
$dbname = "testdb";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn1 = mysqli_connect($servername, $username, $password, $dbname);
$conn2 = new mysqli($servername, $username, $password, $dbname);
var_dump($conn1->stat, $conn2->stat);

Output:

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Access denied for user 'testuser'@'localhost' to database 'testdb'' in /home/public_html/wj-test.php:11 
Stack trace: 
    #0 /home/public_html/wj-test.php(11): mysqli_connect('localhost', 'testuser', 'testpassword', 'testdb') 
    #1 {main} thrown in /home/public_html/wj-test.php on line 11
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Matthew Ediger
  • 313
  • 2
  • 14
  • 1
    I know you said the variables are right, but check them again. It looks like you have `$username = '*'` and `$dbname = '*'` in the first code. Maybe it's a variable scope problem. – Barmar Mar 09 '17 at 04:29
  • just changed them to *'s for this question. – Matthew Ediger Mar 09 '17 at 04:33
  • 1
    There has to be some other difference in the two codes. `mysqli_connect()` is simply a function that calls `new mysqli`. That's how all the procedural vs. OOP variants of `mysqli` work. – Barmar Mar 09 '17 at 04:36
  • Turn on full error reporting in the script, to make sure you're not getting any warnings about undefined variables. – Barmar Mar 09 '17 at 04:37
  • its on ~E_ALL but I tried E_ALL as well and no other errors. I swear I can just swap in mysqli_connect() and it works. Ugh. – Matthew Ediger Mar 09 '17 at 05:02
  • 2
    Maybe switch to PDO :) I think it's much nicer than mysqli. – Barmar Mar 09 '17 at 06:55
  • 1
    As much as I hate leaving a mystery like this unsolved, maybe you should just give in an use `mysqli_connect`, it's not that big a deal. You can still use OOP everywhere else with `$conn`. – Barmar Mar 09 '17 at 07:08
  • What php version are you using? Note that the PHP manual page for `mysqli::$connect_error` states that it "works as of PHP 5.2.9 and 5.3.0." specifically with regard to using it with the OOP constructor. It seems unlikely that you would be using such an old php version, but if you are, then this is probably the answer. (and if you are, then please please upgrade immediately!) – Spudley Oct 18 '18 at 20:53

4 Answers4

-2

Here ,May be two issues :

  • The database password may be wrong or empty password.
  • User permission , this particular user may not having permission for this this DB.

Login to root user (super user) and check the mysql table for user privileges

-2

Use your common sense.

As you probably know, there is no magic in this world. Therefore, if you face some strange, impossible behavior, it means there is a human error to blame.

Obviously, some of your premises are wrong. Most likely, procedural version doesn't work either. So you have to test it. And, if it comes to asking a question, to provide a trustable code that makes an evident test. While from the way you are wording it, you didn't make a side by side comparison.

Why don't you write your variables definition, then procedural, and then OOP variants and then run it at once?

This is what you should be doing, instead of producing some speculations or conjectures.

A robust error reporting instead of blunt die("Connection failed: also helps.

<?php 
ini_set('display_errors',1);
error_reporting(E_ALL);

$servername = "";
$username = "";
$password = "";
$dbname = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn1 = mysqli_connect($servername, $username, $password, $dbname);
$conn2 = new mysqli($servername, $username, $password, $dbname);
var_dump($conn1->stat, $conn2->stat);

This is what you had to post here as a sensible question. You see, telling us verbatim what you did something and swearing what you seen something doesn't work in programming. Only a code does.

So please run this exact code (setting your credentials of course) and copy and paste its output here.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-2

I experienced the exact opposite, i.e. new mysqli() did work for me and mysqli_connect() failed. I solved this by defining the port of the SQL server and changing "localhost" to "127.0.0.1".

So in the above case, try:

$dbport = 3307;
$conn = new mysqli($servername, $username, $password, $dbname, $dbport);

and/or

$servername = "127.0.0.1";

Maybe this issue is related to running MySQL and PHP on a Synology NAS.

-4

What helped me was changing vars into single quotes...

    $servername = 'localhost';
    $username = 'testuser';
    $password = 'testpassword';
    $dbname = 'testdb';

// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

else {
   echo "Connected.";
}

$conn->close();
Joostm
  • 1