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

Popular Categories





How to Run multiple Query in mysqli_query ?

+1 vote
939 views
asked in Programming by payel (1,640 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.
 
 

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.  
 
<?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);
}
 
commented by payel (1,640 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 106 views
+8 votes
1 answer 88 views
+2 votes
1 answer 88 views
+12 votes
1 answer 110 views
+7 votes
7 answers 1,001 views
0 votes
0 answers 430 views
+2 votes
1 answer 861 views
–1 vote
0 answers 109 views
+4 votes
1 answer 843 views
+6 votes
1 answer 126 views

Not a Member yet?

Ask to Folks Login

My Account
672 Folks are online
51 members and 621 guest online
Your feedback is highly appreciated