php - Why I am getting the Error "Commands out of sync; you can't run this command now" -
the documentation of error mentioned in title says
if commands out of sync; can't run command now in client code, calling client functions in wrong order.
this can happen, example, if using mysql_use_result() , try execute new query before have called mysql_free_result(). can happen if try execute 2 queries return data without calling mysql_use_result() or mysql_store_result() in between.
from here: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
but in first query not fetching data mysql database, inserting. , in second query getting data database.
here code
$connection = mysqli_connect("localhost","username","password","tbl_msgs"); if(mysqli_connect_errno($connection)) { die("failed connect mysql: " . mysqli_connect_error()); } $query = "insert users (total_comments, total_views) values ({$total_comments}, {$total_views});"; $query .= "insert msgs (notifications) values ({$notifications})"; mysqli_multi_query($connection,$query);
upto step every thing fine. when execute following query gives error
$select_query = "select * msgs msg_id = {$msg_id}"; $result_set = mysqli_query($connection,$select_query); if(!$result_set) { die(mysqli_error($connection)); }
here gives error commands out of sync; can't run command now
. can't understand situation
note: there problem in query, have executed same query directly phpmyadmin , works fine.
there result set pending query:
mysqli_multi_query($connection,$query);
you need use/store result before can proceed next query after: since don't care first result set, after multi query..
do { $result = mysqli_store_result($connection); mysqli_free_result($result); }while(mysqli_next_result());
another alternative close connection , starts again..
mysqli_close($connection); $connection = mysqli_connect("localhost","username","password","tbl_msgs");
it depends on requirements.
Comments
Post a Comment