Toto je skratena moja trieda mysql
<?
class mysql {
private $linkid;
private $host;
private $user;
private $pswd;
private $db;
private $result;
private $querycount;
function __construct($host,$user,$pswd,$db) {
$this->host=$host;
$this->user=$user;
$this->pswd=$pswd;
$this->db=$db;
}
function connect() {
try {
$this->linkid=@mysql_pconnect($this->host,$this->user,$this->pswd);
if (!$this->linkid) throw new Exception("Nejde pripojiť k serveru MySQL.");
}
catch (Exception $e) {die($e->getMessage());}
}
function select() {
try {
if (!@mysql_select_db($this->db,$this->linkid)) throw new Exception("Nejde sa pripojiť k databázi MySQL");
}
catch (Exception $e) {die($e->getMessage());}
}
function query($query) {
try {
$this->result=@mysql_query($query,$this->linkid);
if (!$this->result) throw new Exception("Databázová požiadavka sa nevydarila.");
}
catch (Exception $e) {die($e->getMessage());}
$this->querycount++;
return $this->result;
}
function fetchObject() {
$row=@mysql_fetch_object($this->result);
return $row;
}
function fetchRow() {
$row=@mysql_fetch_row($this->result);
return $row;
}
function fetchArray() {
$row=@mysql_fetch_array($this->result);
return $row;
}
}
?>
ako mam urobit dvojite tahanie zoznamov z databazy?
trebars mam nejaky zoznam kategorii v databaze a ku kazdej kategorii chcem vypisat zopar clankov... normalne by sa to dalo asi takto:
$mysql->query("select * from kategoria");
while ($row=$mysql->fetchObject()) {
echo "<h2>".$row->nazov."</h2> Zoznam clankov:<br />";
$mysql->query("select * from clanky where kategoria=".$row->id);
while ($row=$mysql->fetchObject()) {
echo $row->nazov."<br />";
}
}
ale mam taky dojem, ze sa mi tam potom premaze $result zo zoznamu kategorii na zoznam clankov... ako mam rozlisit prvy result a druhy??
Ps.: sory, vem ze to velmi nieje zrozumitelne