Running PHP5: Access Denied for MySQLi Connection, but Success for MySQL

I ran this code to get the result shown in the image below:

<?php
  phpinfo();
?>

enter image description here

The problem is that I cannot understand why this works:

$conn = @mysql_connect('host', 'user', 'pass') or die(mysql_error());
      @mysql_select_db('dbName') or die(mysql_error());

      $query="SELECT * FROM Clients";

     $result = @mysql_query($query) or die(mysql_error());
      if ($result)
      {
            $outp = "";
      while ($row = mysql_fetch_assoc($result))
      {

        if ($outp != "") {$outp .= ",";}
        $outp .= '{"ClientID":"'  . $row["cID"] . '",';
        $outp .= '"ClientsName":"'   . $row["clientsName"] . '"}'; 
      }
      mysql_free_result($result);
      }
    $outp ='{"records":['.$outp.']}';
    echo($outp);

But this is not so:

$conn = new mysqli("host", "user", "pass", "dbName");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
$result = $conn->query("SELECT * FROM Clients");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"ClientID":"'  . $rs["cID"] . '",';
    $outp .= '"ClientsName":"'   . $rs["clientsName"] . '"}'; 
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo($outp);

The error I get is:

Connection failed: access denied for user 'user' @ 'host' to database 'dbName'

I read that as long as PHP5 is running, MySQLi is also already installed. What else do I need to check? Maybe I'm doing something wrong in my code? This is my first attempt at MySQLi, I have not connected to the database for years, I had to rummage through the old code to get the old SQL method for connecting to the database.

+4
source share
2 answers

Your code looks great.

, auth .

-, MySQL . root@localhost. , , , root@123.123.123.123, %, root@%.

, .

0

A sql, php mysqli . , mysql ( ) . . .

0

Source: https://habr.com/ru/post/1589103/


All Articles