How comes the second mysql query is executed before the first one ?
-
Hi, I am developing a quite specific plugin involving a voting system and which interfaces with WP-Polls plugin. Basically it works via Rest-API, and after some testing, using it as such seem to work properly.
However during the testing (on a local sever, not online), I encountered a strange issue. When running the vote function (which checks whether the user has already voted, and if he/she didn’t, takes the vote into account) via a WordPress hook (plugin activation) only for debugging purpose.
Here below is the simplified function in which I isolated what creates the situation i am describing, which revolves around the evaluation of $user_has_already_voted.
1. When the database is empty (= user has not voted), the function is actually returning “failure” (instead of “success”) AND creates the database entry. The var_dump of $user_has_already_voted shows the content of $API_answers_ids which is strange since it is called before that the database entry is supposed to be created. Expected behavior would be to have $user_has_already_voted set to false, database entry created, and “success” string returned.
2. In the same situation (empty database), when running the same function with the comment mark removed from L.20 (die('stop now');
), var_dump of $user_has_already_voted shows “false”, and the function does die at “stop_now”, which is the expected behavior.Could someone explain to me why case 1 happens, and what could be done to prevent it ?
Again, strangely enough this doesn’t seem to happen when the function is triggered via the REST API.function mybuggytest($API_film_id, $API_user_id, $API_answers_ids){ global $wpdb; $API_film_id = 112; $API_user_id = 1956; $API_answers_ids = array(47); $result = ''; $user_has_already_voted = ''; //check vote status $API_user_record = $wpdb->get_col( $wpdb->prepare( "SELECT pollip_aid FROM $wpdb->pollsip WHERE pollip_qid = 45 AND pollip_uid_s72 = %d", $API_user_id ) ); if ( $API_user_record ) { //user has already voted $user_has_already_voted = $API_user_record; //answer ID is a positive integer } else { //user has not voted yet $user_has_already_voted = false; } var_dump($user_has_already_voted); //if not already voted, update DB with vote if (empty($user_has_already_voted)){ //die('stop now'); foreach ($API_answers_ids as $polla_aid) { $wpdb->insert( $wpdb->pollsip, array( 'pollip_qid' => 45, 'pollip_aid' => $polla_aid, 'pollip_uid_s72' => $API_user_id ), array( '%d', '%d' ) ); } $result = 'success'; } else { $result = 'failure'; } die($result); }
- The topic ‘How comes the second mysql query is executed before the first one ?’ is closed to new replies.