Hey there! any question in your mind? Ask It Now!.

Popular Categories

How to Run multiple Query in mysqli_query ?

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

1 Answer

+1 vote
answered by nisha (720 points)
selected by payel
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.  
$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);
commented by payel (1,660 points)
Thanks dear.
commented by jack (1,440 points)
hey @nisha good job , i also used your code for the same problem.
commented by nisha (720 points)
Thanks sir , Plasure of mine sir :)

Related Questions

+8 votes
2 answers 222 views
+8 votes
1 answer 243 views
+2 votes
1 answer 205 views
+12 votes
1 answer 238 views
+2 votes
1 answer 1,081 views
–1 vote
0 answers 192 views
+4 votes
1 answer 1,240 views

Not a Member yet?

Ask to Folks Login

My Account
326 Folks are online
0 members and 326 guest online
Your feedback is highly appreciated