witam
napisalem sobie prostą klasę do obsługi MySQL, nie zamieszczam tego jeszcze w dziale oceny ponieważ to nic wielkiego, chciałbym jeszcze nad nią tochę popracować, co radzicie, przeglądałem troche kody podobnych klas, czekam na Wasze opinie
myślę o jakiejś obsłudze błędów ponieważ narazie nie ma to żadnych zabezpieczeń
pzdr
<?
//show_source ('mysql.class.php');
private $host;
private $user;
private $password;
private $database;
public $id_connection;
public function __construct ($host, $user, $password, $database) {
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
if (! ($this->id_connection = mysql_connect ($this->host, $this->user, $this->password))) { throw new Exception ('Nie mogę połączyć się z serwerem bazy danych');
}
throw new Exception ('Nie mogę połączyć się bazą danych: '.$this->database);
}
}
public function make_query($sql) {
return new mysql_res
(mysql_query($sql, $this->id_connection)); }
public function __destruct () {
}
}
class mysql_res implements IteratorAggregate {
private $result;
public function __construct($result) {
$this->result = $result;
}
public function getIterator() {
return new mysql_res_iterator($this->result);
}
}
class mysql_res_iterator implements Iterator {
private $result;
private $current = 0;
private $rows;
public function __construct($result) {
$this->result = $result;
}
}
return $this->current;
}
$this->current = 0;
}
$this->current++;
}
public function valid() {
return $this->current < $this->rows;
}
}
?>
i użycie:
<?
function __autoload($classname) {
include_once $classname.'.class.php';
}
try {
$db = new mysql('localhost','root','krasnal', 'tomek'); $sql = 'SELECT * FROM ks_tel';
$dane = $db->make_query($sql);
foreach ($dane as $id ){
echo 'Imie: '.$id->imie.'<br>'; echo 'Nazwisko: '.$id->nazwisko.'<br>'; echo 'Numer: '.$id->numer.'<br><br>'; }
} catch (Exception $e) {
}
?>
Ten post edytował mynio 22.08.2005, 21:43:07