Je nach Programmlogik würde ich in deinem Fall einen Container schaffen der deine Queries sammelt ( Sinn und Zweck, zugriffsfähigkeit deiner Connection, sollte deine Programmlogik innerhalb der Query-Zusammenstellung bspw. Sub-Queries ausführen wollten kann es dies in einem bestehenden multi-query-set in der selben MySQLi-Instanz nicht, siehe User-Comments zu multi_query ) und ausführt.
Deine For-Schleifen ansich sind auch nicht so das wahre, meiner Meinung nach. Code-technisch stell ich mir das so vor:
PHP-Code:
class multiQuery {
private $_sql;
public function __construct(mysqli &$_sql) {
$this->_sql = &$_sql;
}
private $_queries=array();
public function withCollection(array $queries) {
$this->_queries = array(); // drop all queries
foreach ( $queries as $query ) {
$this->_queries[] = $query;
}
return $this;
}
public function withQuery($query) {
$this->_queries[] = $query;
return $this;
}
private $_results=array();
public function asResultArray() {
$query = join(";", $this->_queries);
$resultset = $this->_sql->multi_query($query);
if ( $resultset ) {
do {
$result = $this->_sql->store_result() ) {
if ( is_object($result) ) $this->_results[] = clone $result;
else $this->_results[] = $result;
$result->free();
} while ( $this->_sql->next_result() );
} else {
return array();
}
return $this->_results;
}
public function asCollection() {
$query = join(";", $this->_queries);
$resultset = $this->_sql->multi_query($query);
if ( $resultset ) {
do {
$result = $this->_sql->store_result();
if ( is_object($result) ) $this->_results[] = clone $result;
else $this->_results[] = $result;
$result->free();
} while ( $this->_sql->next_result() );
}
return $this;
}
public function getQueryCount() {
return count($this->_queries);
}
public function getResultCount() {
return count($this->_results);
}
public function getResult($id) {
$id = (integer)$id;
if ( array_key_exists($id, $this->_results) ) return $this->_results[$id];
else return false;
}
public function getQuery($id) {
$id = (integer)$id;
if ( array_key_exists($id, $this->_queries) ) return $this->_queries[$id];
else return false;
}
public static function onDevice(mysqli &$sql) {
return new multiQueryCollector($sql);
}
}
usage:
PHP-Code:
// multiQuery in the short CI-way, give all results back
$db = new mysqli("localhost","iam", "mypw", "thisdb");
$resultsets = multiQuery::onDevice($db)->withCollection(array(
"UPDATE this SET that='0' WHERE bla=0",
"UPDATE this SET that='0' WHERE bla=1",
"UPDATE this SET that='0' WHERE bla=2",
"UPDATE this SET that='0' WHERE bla=3",
"UPDATE this SET that='0' WHERE bla=4",
"UPDATE this SET that='0' WHERE bla=5"
))->asArray();
// multiQuery in the standard way
$db = new mysqli("localhost", "iam", "mypw", "thisdb");
$mq = new multiQuery($db);
$mq->withCollection(array(
"UPDATE this SET that='0' WHERE bla=0",
"UPDATE this SET that='0' WHERE bla=1",
"UPDATE this SET that='0' WHERE bla=2",
"UPDATE this SET that='0' WHERE bla=3",
"UPDATE this SET that='0' WHERE bla=4",
"UPDATE this SET that='0' WHERE bla=5"
));
$mq->asCollection();
// example for "get the third resultset of all queries"... ( aka index #2 )
$thirdResultset = $mq->getResult(2);
Der Query-Set / Query-Result status innerhalb des mysqli-objects bleibt immer der gleiche, du kannst dieses also locker für normale queries weiternutzen ohne jedesmal etwaige result-sets freizugeben. Die Result-Objekte der eigentlichen multi-queries sind clones, bleiben also nach dem free-ing erhalten bis du das array zerstörst.
Sollte sich hier der ein oder andere Fehler eingeschlichen haben, entschuldige, ich hab das hier im Forum-Editor ohne SH zusammengefummelt.
So würde ich das zumindest realisieren wollen für deinen Fall ( fehler-behandlung hab ich hier mal außen vor gelassen.. ).