Saturday, 15 June 2013

How do I get the results from mysqli multi query? -



How do I get the results from mysqli multi query? -

my query is

create temporary table `hcaconsumptions_temp` ( `deviceid` int (11) not null, `timestamp` timestamp not null default current_timestamp on update current_timestamp, `consumption` float not null, `devicebrand` varchar (255) character set utf8 collate utf8_unicode_ci not null, `serialnumber` varchar (255) character set utf8 collate utf8_unicode_ci not null ) engine = myisam default character set = utf8 collate = utf8_unicode_ci; insert hcaconsumptions_temp ( devicebrand, serialnumber, `timestamp`, consumption ) values ( 'adunos', '24100008', '2013-01-14 19:39:48', '157' ), ( 'adunos', '24100010', '2013-01-14 18:50:38', '134' ), ( 'adunos', '24100037', '2013-01-14 18:41:29', '119' ), ( 'adunos', '24100014', '2013-01-14 18:41:12', '141' ), ( 'adunos', '24100015', '2013-01-14 18:41:01', '137' ), ( 'adunos', '24100034', '2013-01-14 18:41:17', '139' ), ( 'adunos', '24100035', '2013-01-14 18:41:24', '139' ), ( 'adunos', '24100041', '2013-01-14 18:40:41', '136' ), ( 'adunos', '24100042', '2013-01-14 18:41:03', '144' ), ( 'adunos', '24100043', '2013-01-14 18:39:49', '139' ), ( 'adunos', '24100045', '2013-01-14 18:40:20', '137' ), ( 'adunos', '24100001', '2013-01-14 18:40:51', '121' ), ( 'adunos', '24100002', '2013-01-14 18:41:20', '1' ), ( 'adunos', '24100003', '2013-01-14 18:41:46', '132' ), ( 'adunos', '24100004', '2013-01-14 18:41:57', '126' ), ( 'adunos', '24100005', '2013-01-14 18:46:02', '165' ), ( 'adunos', '24100006', '2013-01-14 18:39:54', '177' ), ( 'adunos', '24100007', '2013-01-14 18:40:49', '171' ), ( 'adunos', '24100021', '2013-01-14 18:41:14', '132' ), ( 'adunos', '24100073', '2013-01-14 18:43:00', '161' ), ( 'adunos', '24100074', '2013-01-14 18:41:14', '13' ), ( 'adunos', '24100076', '2013-01-14 18:41:27', '75' ), ( 'adunos', '24100078', '2013-01-14 18:39:44', '154' ), ( 'adunos', '24100079', '2013-01-14 18:43:42', '49' ), ( 'adunos', '24100022', '2013-01-14 18:40:31', '6' ), ( 'adunos', '24100023', '2013-01-14 18:44:25', '42' ), ( 'adunos', '24100024', '2013-01-14 18:42:19', '12' ), ( 'adunos', '24100072', '2013-01-14 18:40:36', '45' ), ( 'adunos', '24100025', '2013-01-14 18:40:00', '52' ), ( 'adunos', '24100026', '2013-01-14 18:40:53', '53' ), ( 'adunos', '24100029', '2013-01-14 18:41:29', '22' ), ( 'adunos', '24100030', '2013-01-14 18:39:28', '61' ), ( 'adunos', '24100031', '2013-01-14 18:41:49', '6' ), ( 'adunos', '24100027', '2013-01-14 18:40:39', '60' ), ( 'adunos', '24100028', '2013-01-14 18:40:02', '119' ), ( 'adunos', '24100071', '2013-01-14 18:40:15', '127' ), ( 'adunos', '24100011', '2013-01-14 18:41:28', '155' ), ( 'adunos', '24100012', '2013-01-14 18:40:19', '145' ), ( 'adunos', '24100013', '2013-01-14 18:41:19', '139' ), ( 'adunos', '24100016', '2013-01-14 18:40:28', '149' ), ( 'adunos', '24100018', '2013-01-14 18:40:20', '156' ), ( 'adunos', '24100036', '2013-01-14 18:41:30', '148' ), ( 'adunos', '24100044', '2013-01-14 18:43:12', '98' ), ( 'adunos', '24100009', '2013-01-14 18:40:41', '129' ), ( 'adunos', '24100032', '2013-01-14 19:11:17', '156' ), ( 'adunos', '24100033', '2013-01-14 18:39:53', '136' ), ( 'adunos', '24100019', '2013-01-14 18:40:58', '117' ), ( 'adunos', '24100020', '2013-01-14 18:42:22', '74' ); update hcaconsumptions_temp set deviceid = ( select deviceid hcadevices hcadevices.devicebrand = hcaconsumptions_temp.devicebrand , hcadevices.serialnumber = hcaconsumptions_temp.serialnumber ); select count(deviceid) hcaconsumptions_temp deviceid = '0';

as can see @ end there select query cannot results. because there 3 queries gives error. how i'll result?

my php code is

$test_result = mysqli_multi_query($link, $multi_test_query); $count = mysqli_fetch_assoc($test_result); print_r($count);

the warning given mysqli_fetch_assoc() expects parameter 1 mysqli_result, boolean given.

this code gives 2 errors:

$test_result = mysqli_multi_query($link, $multi_test_query); $count = mysqli_fetch_assoc($test_result); echo mysqli_error(); print_r($count); <br /> <b>warning</b>: mysqli_fetch_assoc() expects parameter 1 mysqli_result, boolean given in <b>c:\program files (x86)\easyphp-12.1\www\app\php\post\excel_yukle.php</b> on line <b>123</b><br /> <br /> <b>warning</b>: mysqli_error() expects 1 parameter, 0 given in <b>c:\program files (x86)\easyphp-12.1\www\app\php\post\excel_yukle.php</b> on line <b>124</b><br />

edit: have tried this:

/* execute multi query */ if (mysqli_multi_query($link, $multi_test_query)) { { /* store first result set */ if ($result = mysqli_store_result($link)) { while ($row = mysqli_fetch_row($result)) { printf("%s\n", $row[0]); } mysqli_free_result($result); } /* print divider */ if (mysqli_more_results($link)) { printf("-----------------\n"); } } while (mysqli_next_result($link)); }

and result was:

----------------- ----------------- ----------------- 46 <br /> <b>strict standards</b>: mysqli_next_result(): there no next result set. please, phone call mysqli_more_results()/mysqli::more_results() check whether phone call function/method in <b>c:\program files (x86)\easyphp-12.1\www\hcawebapp\php\post\excel_yukle.php</b> on line <b>136</b><br />

the "answer" provided/selected poster not solution, rather workaround not beingness able find solution. whole $i business should removed; don't see type of work @ of mysqli docs pages. not type of coding programmers should sharing/emulating.

of queries running, 1 of them should have record set.

create temporary table //returns true/false. *structural, no rows in case. insert //returns true/false. *mysqli_affected_rows if desired. update //returns true/false. *mysqli_affected_rows if desired. select //returns record set. *max of 1 row in case.

my proposed solution is:

if(mysqli_multi_query($link,$multi_test_query)){ do{ if($result=mysqli_store_result($link)){ // record set true $satirsayisi=current(mysqli_fetch_row($result)); mysqli_free_result($result); }else{ // record set false, no errors //echo "<br>affected rows: mysqli_affected_rows($link); } } while(mysqli_more_results($link) && mysqli_next_result($link)); if($error_mess=mysqli_error($link)){ $satirsayisi="loop breaking error: $error_mess"; } }else{ $satirsayisi="first query error: ".mysqli_error($link); } echo $satirsayisi;

on success, $satirsayisi count value, otherwise show first error multi_query caused (and subsequent querying cease).

additional advice:

incrementing: in effort utilize minimal lines, remove $i=1, , replace $i=$i+1 ++$i

fetching single-value-result: using current() mysqli_fetch_row() gets job done in 1 line; no while loop needed.

looping mysqli_multi_query(): reference docs utilize while loops loop entered on first iteration, , status statement checked @ end of each iteration.

mysqli mysqli-multi-query

No comments:

Post a Comment