Question

I'm looking for an explanation / fix for this error. I'm using the following script. This is my first time using multi_query :

        $storyidr=$_POST['storyidr'];
$mysqli = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server.");
 if (mysqli_connect_errno($mysqli))
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
$sql  = "INSERT INTO ratings (storyidr, rank, entry_date) VALUES ('$_POST[storyidr]','$_POST[value]',now());"; 
$sql .= "SELECT AVG(rank) AS avrank from ratings WHERE storyidr = $storyidr";
if($mysqli->multi_query($sql))
{   
if ($result = $mysqli->store_result())
{ 
$data = mysqli_fetch_assoc($result);
$avrank = $data['avrank'];
        if(!$result)
        {
              $arr = array ('status'=>'fail');
              echo json_encode($arr);
        }
        else
        {

                echo json_encode($avrank);
        }
                exit;
}
}
Was it helpful?

Solution

The variable $mysqli is not defined, you are assigning it to $con, Change from

$con = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server.");

to

$mysqli = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top