Esempio n. 1
0
 function tuoteHaku($id)
 {
     $sql = "SELECT * FROM tuote WHERE id like :id";
     //valmistellaan lause
     if (!($stmt = $this->db->prepare($sql))) {
         $virhe = $this->db->errorInfo();
         throw new PDOException($virhe[2], $virhe[1]);
     }
     //sidotaan parametrit
     $idd = "%" . utf8_decode($id) . "%";
     $stmt->bindValue(":id", $idd, PDO::PARAM_INT);
     //ajetaan lause
     if (!$stmt->execute()) {
         $virhe = $stmt->errorInfo();
         if ($virhe[0] == "HY093") {
             $virhe[2] = "Invalid parameter";
         }
         throw new PDOException($virhe[2], $virhe[1]);
     }
     //käsitellään haun tulokset
     $haettuTuote = array();
     while ($row = $stmt->fetchObject()) {
         // Tehdään tietokannasta haetusta rivistä tuote-luokan olio
         $tuote = new Tuote();
         $tuote->setId($row->id);
         $tuote->setNimi(utf8_encode($row->nimi));
         $tuote->setKuvaus(utf8_encode($row->kuvaus));
         $tuote->setHinta($row->hinta);
         $tuote->setKategoria($row->kategoria);
         $tuote->setKuva(utf8_encode($row->kuva));
         $tuote->setValmistaja(utf8_encode($row->valmistaja));
         // Laitetaan olio tulos taulukkoon (olio-taulukkoon)
         $haettuTuote[] = $tuote;
     }
     $this->lkm = $stmt->rowCount();
     return $haettuTuote;
 }