I got error when run php code while connecting to mysql. Please help me.
Error given below...
( ! ) Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\Ampps\www\Piyush\test.php on line 21
Call Stack
( ! ) Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\Ampps\www\Piyush\test.php on line 26
Call Stack
my php script is given below
<?php
$databasehost = "localhost";
$databasename = "PiyushDB";
$databaseusername ="root";
$databasepassword = "Piyush@001";
$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);
if (mysqli_errno($con)) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysqli_error($con);
}
else
{
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
$res = json_encode($rows);
echo $res;
mysqli_free_result($sth);
}
mysqli_close($con);
?>
Error given below...
( ! ) Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\Ampps\www\Piyush\test.php on line 21
Call Stack
( ! ) Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\Ampps\www\Piyush\test.php on line 26
Call Stack
my php script is given below
<?php
$databasehost = "localhost";
$databasename = "PiyushDB";
$databaseusername ="root";
$databasepassword = "Piyush@001";
$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);
if (mysqli_errno($con)) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysqli_error($con);
}
else
{
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
$res = json_encode($rows);
echo $res;
mysqli_free_result($sth);
}
mysqli_close($con);
?>