+1 vote
1.4k views
in Programming by (1.7k points)
Hi All I am trying to update 2 tables together in in results.
 
like   
 
$result = mysqli_query($con,$query1,$query2);
 
But both queries are not getting executed.Please help me on this.
 
 
closed

1 Answer

+1 vote
by (720 points)
selected by
 
Best answer
Here you can see we are using 2 queries and executing them with mysqli_multi_query  and result we also verifying which has pass and fail,I hope you got my point for running multiple query.  
 
<?php
 
$mysqli = new mysqli($host, $user, $password, $database);
 
// create string of queries separated by ;
$query  = "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName');";
$query .= "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year');";
 
// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);
 
if ($result) {
    do {
        // grab the result of the next query
        if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
            echo "Query failed: " . mysqli_error($mysqli);
        }
    } while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
} else {
    echo "First query failed..." . mysqli_error($mysqli);
}
 
by (1.7k points)
Thanks dear.
by (1.4k points)
hey @nisha good job , i also used your code for the same problem.
by (720 points)
Thanks sir , Plasure of mine sir :)

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated