<?
class MySQLiQueryIterator implements Iterator {
protected $link;
protected $query;
protected $key;
protected $valid;
protected $result;
public function __construct($link, $query) {
$this->link = $link;
$thisquery = $query;
}
$this->key = 0;
if (mysqli_multi_query($this->link, $this->query) ) {
$this->result = mysqli_store_result($this->link);
$this->valid = true;
} else {
$this->result = false;
$this->valid = false;
}
}
public function valid() {
//mysqli_more_results() wyprzedza valid() klasy iterator o jedna pozycje
$valid = $this->valid;
$this->valid = mysqli_more_results($this->link);
return $valid;
}
return $this->key;
}
return $this->result;
}
if ($this->result) { mysqli_free_result($thos->result); }
$this->key++;
if (mysqli_next_result($this->link) ) {
$this->result = mysqli_store_result($this->link);
} else {
$this->result = false;
}
}
}
$db = mysqli_connect('localhost', 'hash', 'haslo', 'thc');
$query = >>>_SQL_
DROP TABLE IF EXISTS users;
CREATE TABLE users(username VARCHAR(50) UNIQUE,
password VARCHAR(50) );
INSERT INTO users VALUES('rasmus', 'zsjdsank');
INSERT INTO users VALUES('zeev', 'zjsdnaka');
SELECT username FROM users;
_SQL_;
foreach (new MySQLiQueryIterator($db, $query) as $result) {
if($result) {
while ($row = mysqli_fetch_row($result) ) {
}
}
}
?>
zgłasza że w lini 60:
Parse error: syntax error, unexpected T_SR in c:\wamp\www\phpproject\iterator.php
to jest przyklad z ksiazki ;] php nowe mozliwosci, w sumie nie wiem czemu uzywa sie tutaj >>> (co to operator biotwy jakis?) i wogole zadnych cudzyslowow, jak probowalem wlozyc cudzyslowy, to wogole nie wykonuje tego zapytania (cos zle w klasie? czy co.. ?) wkurzam sie powoli na ta ksiazke, juz z dwa razy blad wychwycilem w tych kodach (literowka, a drugi zla nazwa funkcji), teraz albo nie widze gdzie jest blad(troszke w pospiechu czytam ksiazke, moze cos przeoczylem), albo znowu cos w ksiazce zwalili.
ps. fajne to php 5 (skladnie to z JAVY wyjeli ;] ), ale po 4 mozna sie pogubic ;]