@@ -184,7 +184,7 @@ public function parse($data)
184184 $ fieldCount = ord ($ this ->read (1 ));
185185field:
186186 if ($ fieldCount === 0xFF ) {
187- //error packet
187+ // error packet
188188 $ u = unpack ('v ' , $ this ->read (2 ));
189189 $ this ->errno = $ u [1 ];
190190 $ state = $ this ->read (6 );
@@ -193,7 +193,8 @@ public function parse($data)
193193
194194 $ this ->nextRequest ();
195195 $ this ->onError ();
196- } elseif ($ fieldCount === 0x00 ) { //OK Packet Empty
196+ } elseif ($ fieldCount === 0x00 ) {
197+ // Empty OK Packet
197198 $ this ->debug ('Ok Packet ' );
198199
199200 $ isAuthenticated = false ;
@@ -213,26 +214,39 @@ public function parse($data)
213214
214215 $ this ->message = $ this ->read ($ this ->pctSize - $ len + $ this ->length ());
215216
216- if ($ isAuthenticated ) {
217- $ this ->onAuthenticated ();
217+ if ($ this ->rsState === self ::RS_STATE_ROW ) {
218+ // Empty OK packet during result set => row with only empty strings
219+ $ row = array ();
220+ foreach ($ this ->resultFields as $ field ) {
221+ $ row [$ field ['name ' ]] = '' ;
222+ }
223+ $ this ->onResultRow ($ row );
218224 } else {
219- $ this ->onSuccess ();
225+ // otherwise this terminates a query without a result set (UPDATE, INSERT etc.)
226+ if ($ isAuthenticated ) {
227+ $ this ->onAuthenticated ();
228+ } else {
229+ $ this ->onSuccess ();
230+ }
231+ $ this ->debug (sprintf ("AffectedRows: %d, InsertId: %d, WarnCount:%d " , $ this ->affectedRows , $ this ->insertId , $ this ->warnCount ));
232+ $ this ->nextRequest ();
220233 }
221- $ this ->debug (sprintf ("AffectedRows: %d, InsertId: %d, WarnCount:%d " , $ this ->affectedRows , $ this ->insertId , $ this ->warnCount ));
222- $ this ->nextRequest ();
223-
224- } elseif ($ fieldCount === 0xFE ) { //EOF Packet
234+ } elseif ($ fieldCount === 0xFE ) {
235+ // EOF Packet
225236 $ this ->debug ('EOF Packet ' );
226237 if ($ this ->rsState === self ::RS_STATE_ROW ) {
238+ // finalize this result set (all rows completed)
227239 $ this ->debug ('result done ' );
228240
229241 $ this ->nextRequest ();
230242 $ this ->onResultDone ();
231243 } else {
232- ++ $ this ->rsState ;
244+ // move to next part of result set (header->field->row)
245+ ++$ this ->rsState ;
233246 }
234247
235- } else { //Data packet
248+ } else {
249+ // Data packet
236250 $ this ->debug ('Data Packet ' );
237251 $ this ->prepend (chr ($ fieldCount ));
238252
@@ -266,24 +280,29 @@ public function parse($data)
266280 $ field ['decimals ' ] = ord ($ this ->read (1 ));
267281 //var_dump($field);
268282 $ this ->resultFields [] = $ field ;
269-
270283 } elseif ($ this ->rsState === self ::RS_STATE_ROW ) {
271284 $ this ->debug ('Row packet of Data packet ' );
272285 $ row = [];
273- for ($ i = 0 , $ nf = sizeof ( $ this ->resultFields ); $ i < $ nf ; ++ $ i ) {
274- $ row [$ this -> resultFields [ $ i ] ['name ' ]] = $ this ->parseEncodedString ();
286+ foreach ($ this ->resultFields as $ field ) {
287+ $ row [$ field ['name ' ]] = $ this ->parseEncodedString ();
275288 }
276- $ this ->resultRows [] = $ row ;
277- $ command = $ this ->queue ->dequeue ();
278- $ command ->emit ('result ' , array ($ row , $ command , $ command ->getConnection ()));
279- $ this ->queue ->unshift ($ command );
289+ $ this ->onResultRow ($ row );
280290 }
281291 }
282292 }
283293 $ this ->restBuffer ($ this ->pctSize - $ len + $ this ->length ());
284294 goto packet;
285295 }
286296
297+ private function onResultRow ($ row )
298+ {
299+ // $this->debug('row data: ' . json_encode($row));
300+ $ this ->resultRows [] = $ row ;
301+ $ command = $ this ->queue ->dequeue ();
302+ $ command ->emit ('result ' , array ($ row , $ command , $ command ->getConnection ()));
303+ $ this ->queue ->unshift ($ command );
304+ }
305+
287306 protected function onError ()
288307 {
289308 $ command = $ this ->queue ->dequeue ();
0 commit comments