php mysql commands out of sync

Solutions on MaxInterview for php mysql commands out of sync by the best coders in the world

showing results for - "php mysql commands out of sync"
Maria José
10 Oct 2020
1<?php
2    
3/**
4 *	Free all resultsets from $dbCon. Object Oriented
5 * Originally from https://falseisnotnull.wordpress.com/2013/06/02/mariadbmysql-commands-out-of-sync-error/
6 *	@param		mysqli		$dbCon	mysqli object.
7 *	@return		void
8 */
9function free_all_results(mysqli $dbCon)
10{
11    do {
12        if ($res = $dbCon->store_result()) {
13            $res->fetch_all(MYSQLI_ASSOC);
14            $res->free();
15        }
16    } while ($dbCon->more_results() && $dbCon->next_result());
17}
18    
19$db = new mysqli('...', '...', '...', '...');
20    
21$sql = <<<SQL
22    DROP TABLE IF EXISTS `test`.`tab1`;
23    CREATE TABLE `test`.`tab1` (`col` INT);
24    INSERT INTO `test`.`tab1` VALUE (1);
25SQL;
26$db->multi_query($sql);
27    
28free_all_results($db);
29    
30$sql = 'SELECT * FROM `test`.`tab1`;';
31$res = $db->query($sql);
32    
33if ($db->sqlstate !== '00000') {
34    echo 'SQLSTATE: ' . $db->sqlstate . '; Error: ' . $db->errno . ' - ' . $db->error;
35} else {
36    echo $res->num_rows;
37}
38
39
40/**
41 * Free all resultsets from $dbCon. Procedural
42 * @param mysqli $dbCon mysqli object.
43 * @return        void
44 */
45function free_all_results(mysqli $dbCon): void {
46	do {
47		if ($res = mysqli_store_result($dbCon)) {
48			// $res->fetch_all(MYSQLI_ASSOC);
49			mysqli_fetch_all($res, MYSQLI_ASSOC);
50			mysqli_free_result($res);
51
52		}
53	} while (mysqli_more_results($dbCon) && mysqli_next_result($dbCon));
54}
55
56
57
58
59
60
61?>