I have an insert sql query in a php script. The query executes in phpmyadmin, but when i place it in the php script,it does not execute. The error I get is "Commands out of sync; you can't run this command now". Any inputs on what may be wrong?
The following is the php code:
<?php
$request_method = $_SERVER['REQUEST_METHOD'];
if ($request_method == 'POST') {
//retrieve request body
$requestBody = file_get_contents('php://input');
//write to stats file
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/stats/upload/resourceStats.json', 'a') or die("unable to open file\n");
fwrite($fp, $requestBody, strlen($requestBody));
fclose($fp);
//parsing config file
$monitoring_db_config = parse_ini_file("mySql/monitoringConfig.ini");
//Connect to DB
$mysqli_con = mysqli_connect($monitoring_db_config['host'],$monitoring_db_config['username'],$monitoring_db_config['password']);
if (mysqli_connect_error()) {
die('Connect Error\n');
}
//execute .sql file
$multi_query = file_get_contents("mySql/monitoring.sql");
if(mysqli_multi_query( $mysqli_con , $multi_query) == FALSE ){
echo "multi query failed"."\n";
}
//Insert stats into table
$arr_stat = json_decode($requestBody, true);
$sql_query = "INSERT INTO server_resource_stats (server_id,time,resource,measure,unit) VALUES (1,100,'resource',100,'%');";
if (mysqli_query($mysqli_con,$sql_query) != TRUE) {
echo "error inserting into table\n";
}
//close DB connection
$mysqli->close($mysqli_con);
}
?>
The .sql file is as follows:
/*Create database 'monitoring'*/
CREATE DATABASE IF NOT EXISTS monitoring;
/*Use database 'monitoring'*/
USE monitoring;
/*Create table 'server_resource_stats'*/
CREATE TABLE IF NOT EXISTS server_resource_stats(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,server_id INT(3) NOT NULL,time INT(12) NOT NULL,resource VARCHAR(20) NOT NULL, measure FLOAT(12,4) NOT NULL, unit VARCHAR(10) NOT NULL);