-2

I am trying to get my PHP code to connect to my database hosted on the same server, but no matter what I do, it always ends up returning an empty set.

$servername = "localhost";
                $username = "user";
                $password = "pass";
                $dbnom = "name";

                $conn = new mysqli($servername, $username, $password, $dbnom);

                mysqli_set_charset($conn, "utf8");

                if(!$conn) {
                    die("La connexion a échoué : " . mysqli_connect_error());
                }

                $championsSQL = "SELECT pa_championId FROM participants";
                $resultSQL = mysqli_query($conn, $championsSQL);

                if(mysqli_num_rows($resultSQL) > 0) {
                    while($row = mysqli_fetch_assoc($resultSQL)) {
                        echo "Nom des champions : " . $row["pa_championId"] . "<br>";
                    }
                }
                else {
                    echo "Aucun résultat";
                }
                var_dump($resultSQL);
                mysqli_close($conn);

The MySQL request "SELECT pa_championId FROM participants" works when I enter it manually into the mysql command line on the server. The var_dump at the end always returns NULL. The PHP is from the "Select Data With MySQLi section of this website : https://www.w3schools.com/php/php_mysql_select.asp

I know this question has been posted many times before, but none of the answers I found fit my exact situation.

Logs : https://pastebin.com/M2GCQe4D

1 Answers1

4

My best guess is that the connection failed but your $conn wasn't empty so it never enteres this condition:

if(!$conn) {
     die("La connexion a échoué : " . mysqli_connect_error());
}

You should try using this to see if it returned any errors

if ($conn->connect_error) {
    die("La connexion a échoué : " . $conn->connect_error);
}
Liora Haydont
  • 1,252
  • 1
  • 12
  • 25
  • isn't that what they're doing now? seems like 4 quarters against a dollar – Funk Forty Niner Dec 05 '17 at 15:03
  • 3
    @Fred-ii- - Actually, `new mysqli()` still returns a mysqli object if the connection fails, so just testing `if (!$conn)` (like the OP is currently doing) isn't enough. – M. Eriksson Dec 05 '17 at 15:06
  • @MagnusEriksson the OP should mark it as the correct answer then. ;-) Ok, I'm out now. – Funk Forty Niner Dec 05 '17 at 15:06
  • This returns access denied indeed. I'll look into my credentials. Thanks. – Arnaud Lesueur Dec 05 '17 at 15:09
  • 1
    @MagnusEriksson I can't see how that answer warrants an upvote if it doesn't solve the question. If the connection failed, the OP would see some type of error, but maybe I'm just too stoned right now to notice the difference. – Funk Forty Niner Dec 05 '17 at 15:09
  • 1
    @Fred-ii- I didn't say it did. I simply said why this answer was different from what the OP was currently doing (so it's not really 4 quarters against a dollar). :-) – M. Eriksson Dec 05 '17 at 15:10
  • 1
    My credentials were wrong indeed, I'm sorry for wasting your time guys... – Arnaud Lesueur Dec 05 '17 at 15:11