I ran this code to get the output shown in image below:
<?php
phpinfo();
?>
Trouble is, I cannot figure out 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 does not:
$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 that 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 might I need to check for? Maybe I am doing something wrong in my code? This is my first attempt at MySQLi, I haven't connected to a database in years, had to rummage through old code just to get the old SQL way to connect to database.