phpmysqlmulti-query

multi_query() has an error


I need some help finding my error on the enclosed code. When I run either of the two queries using the if ($conn->query($sql) === TRUE) { method each works correctly. But when I try to combine them with the if ($conn->multi_query($sql) === TRUE) { method. No records are uploaded. What am I doing wrong here.

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";

$payload_dump = $_POST['payload'];
echo $payload_dump;

$payload_array = json_decode($payload_dump,true);

if(is_array($payload_array)){
foreach($payload_array as $row){

//get the data_payload details

$device = $row['device'];
$type = $row['data_type'];
$zone = $row['zone'];
$sample = $row['sample'];
$count = $row['count'];
$time = $row['date_time'];
$epoch = $row['epoch_stamp'];

$sql = "INSERT INTO data(device, type, zone, sample, count, date_time, epoch_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch');";
$sql . = "UPDATE data SET date_time = FROM_UNIXTIME(epoch_stamp);";
if ($conn->multi_query($sql) === TRUE) {
//if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
?>

... and yes I realize this code is not secure but it's ok for my testing purposes.


Solution

  • Intrinsically the code below is the same until we get to the loop where we build up an array of queries to be executed and execute the multi_query() once at the end once we leave the loop. I have removed some of the comments and statements that echo out info at the start for brevity. I hope this looks ok and works....

    <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "practice";
    
        $conn = new mysqli($servername, $username, $password, $dbname);
        if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);
        $payload_dump = $_POST['payload'];
    
        $payload_array = json_decode($payload_dump,true);
    
        if( is_array( $payload_array ) ){
    
            $queries=array();
    
            foreach( $payload_array as $row ){
                //get the data_payload details
                $device = $row['device'];
                $type = $row['data_type'];
                $zone = $row['zone'];
                $sample = $row['sample'];
                $count = $row['count'];
                $time = $row['date_time'];
                $epoch = $row['epoch_stamp'];
    
                /*note: we do not need to add the semi-colon here as it gets added later when we implode the array */
                $queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
            }
            /*
                Previously the below query was being execute on every iteration
                ~ because $epoch is now the last one encountered in the array, 
                the value that is updated in ALL records is as it would have been
                previously.
            */
            $queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch );";
    
            $sql=implode( ';', $queries );
            if ( $conn->multi_query( $sql ) === TRUE ) {
                echo "New records created and updated successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
        $conn->close();
    
    ?>