Advertisement
Guest User

PDO

a guest
May 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. <?php
  2. require_once "asiakastieto.php";
  3.  
  4. class asiakastietoPDO {
  5. private $db;
  6.  
  7. function __construct($dsn = "mysql:host=localhost;dbname=a1500882", $user = "root", $password = "") {
  8. // Ota yhteys kantaan
  9. $this->db = new PDO ( $dsn, $user, $password );
  10.  
  11. // Virheiden jäljitys kehitysaikana
  12. $this->db->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  13.  
  14. // MySQL injection estoon (paramerit sidotaan PHP:ssä ennen SQL-palvelimelle lähettämistä)
  15. $this->db->setAttribute ( PDO::ATTR_EMULATE_PREPARES, false );
  16. }
  17.  
  18. public function kaikkiAsiakkaat() {
  19. $sql = "SELECT id, nimi, katuosoite, postinumero, postitmp, email, puhnro, ika, tavoitteet, kokemus, taajuus, vammat
  20. FROM asiakas";
  21.  
  22. // Valmistellaan lause
  23. if (! $stmt = $this->db->prepare ( $sql )) {
  24. $virhe = $this->db->errorInfo ();
  25.  
  26. throw new PDOException ( $virhe [2], $virhe [1] );
  27. }
  28.  
  29. // Ajetaan lauseke
  30. if (! $stmt->execute ()) {
  31. $virhe = $stmt->errorInfo ();
  32.  
  33. throw new PDOException ( $virhe [2], $virhe [1] );
  34. }
  35.  
  36. // Käsittellään hakulausekkeen tulos
  37. $tulos = array ();
  38.  
  39. // Pyydetään haun tuloksista kukin rivi kerrallaan
  40. while ( $row = $stmt->fetchObject () ) {
  41. // Tehdään tietokannasta haetusta rivistä leffa-luokan olio
  42. $asiakastieto = new Asiakastieto();
  43.  
  44. $asiakastieto->setId ( $row->id );
  45. $asiakastieto->setNimi ( utf8_encode ( $row->nimi ) );
  46. $asiakastieto->setKatuosoite ( utf8_encode ( $row->katuosoite ) );
  47. $asiakastieto->setPostinumero ( utf8_encode ( $row->postinumero ) );
  48. $asiakastieto->setPostitmp ( $row->postitmp );
  49. $asiakastieto->setEmail ( utf8_encode ( $row->email ) );
  50. $asiakastieto->setPuhnro ( utf8_encode ( $row->puhnro ) );
  51. $asiakastieto->setIka ( utf8_encode ( $row->ika ) );
  52. $asiakastieto->setTavoitteet ( utf8_encode ( $row->tavoitteet ) );
  53. $asiakastieto->setKokemus ( utf8_encode ( $row->kokemus ) );
  54. $asiakastieto->setTaajuus ( utf8_encode ( $row->taajuus ) );
  55. $asiakastieto->setVammat ( utf8_encode ( $row->vammat ) );
  56.  
  57. // Laitetaan olio tulos taulukkoon (olio-taulukkoon)
  58. $tulos [] = $asiakastieto;
  59. }
  60.  
  61. $this->lkm = $stmt->rowCount ();
  62.  
  63. return $tulos;
  64. }
  65.  
  66. public function etsiAsiakas($id) {
  67. $sql = "SELECT id, nimi, katuosoite, postinumero, postitmp, email, puhnro, ika, tavoitteet, kokemus, taajuus, vammat
  68. FROM asiakas
  69. WHERE id = :id";
  70.  
  71. // Valmistellaan lause, prepare on PDO-luokan metodeja
  72. if (! $stmt = $this->db->prepare ( $sql )) {
  73. $virhe = $this->db->errorInfo ();
  74. throw new PDOException ( $virhe [2], $virhe [1] );
  75. }
  76.  
  77. // Sidotaan parametrit
  78. $stmt->bindValue ( ":id", $id, PDO::PARAM_INT );
  79.  
  80. // Ajetaan lauseke
  81. if (! $stmt->execute ()) {
  82. $virhe = $stmt->errorInfo ();
  83.  
  84. if ($virhe [0] == "HY093") {
  85. $virhe [2] = "Invalid parameter";
  86. }
  87.  
  88. throw new PDOException ( $virhe [2], $virhe [1] );
  89. }
  90.  
  91. // Käsittellään hakulausekkeen tulos
  92. $tulos = array ();
  93.  
  94. while ( $row = $stmt->fetchObject () ) {
  95. // Tehdään tietokannasta haetusta rivistä leffa-luokan olio
  96. $asiakastieto = new Asiakastieto ();
  97.  
  98. $asiakastieto->setId ( $row->id );
  99. $asiakastieto->setNimi ( utf8_encode ( $row->nimi ) );
  100. $asiakastieto->setKatuosoite ( utf8_encode ( $row->katuosoite ) );
  101. $asiakastieto->setPostinumero ( utf8_encode ( $row->postinumero ) );
  102. $asiakastieto->setPostitmp ( $row->postitmp );
  103. $asiakastieto->setEmail ( utf8_encode ( $row->email ) );
  104. $asiakastieto->setPuhnro ( utf8_encode ( $row->puhnro ) );
  105. $asiakastieto->setIka ( utf8_encode ( $row->ika ) );
  106. $asiakastieto->setTavoitteet ( utf8_encode ( $row->tavoitteet ) );
  107. $asiakastieto->setKokemus ( utf8_encode ( $row->kokemus ) );
  108. $asiakastieto->setTaajuus ( utf8_encode ( $row->taajuus ) );
  109. $asiakastieto->setVammat ( utf8_encode ( $row->vammat ) );
  110.  
  111. // Laitetaan olio tulos taulukkoon (olio-taulukkoon)
  112. $tulos [] = $asiakastieto;
  113. }
  114. return $tulos;
  115. }
  116.  
  117. function lisaaAsiakastieto($asiakastieto) {
  118. $sql = "insert into asiakas (nimi, katuosoite, postinumero, postitmp, email, puhnro, ika, tavoitteet, kokemus, taajuus, vammat)
  119. values (:nimi, :katuosoite, :postinumero, :postitmp, :email, :puhnro, :ika, :tavoitteet, :kokemus, :taajuus, :vammat)";
  120.  
  121. // Valmistellaan SQL-lause
  122. if (! $stmt = $this->db->prepare ( $sql )) {
  123. $virhe = $this->db->errorInfo ();
  124. throw new PDOException ( $virhe [2], $virhe [1] );
  125. }
  126.  
  127. // Parametrien sidonta
  128. $stmt->bindValue ( ":nimi", utf8_decode ( $asiakastieto->getNimi() ), PDO::PARAM_STR );
  129. $stmt->bindValue ( ":katuosoite", utf8_decode ( $asiakastieto->getKatuosoite() ), PDO::PARAM_STR );
  130. $stmt->bindValue ( ":postinumero", utf8_decode ( $asiakastieto->getPostinumero() ), PDO::PARAM_STR );
  131. $stmt->bindValue ( ":postitmp", utf8_decode ( $asiakastieto->getPostitmp() ), PDO::PARAM_STR );
  132. $stmt->bindValue ( ":email", utf8_decode ( $asiakastieto->getEmail() ), PDO::PARAM_STR );
  133. $stmt->bindValue ( ":puhnro", utf8_decode ( $asiakastieto->getPuhnro() ), PDO::PARAM_STR );
  134. $stmt->bindValue ( ":ika", $asiakastieto->getIka(), PDO::PARAM_INT );
  135. $stmt->bindValue ( ":tavoitteet", utf8_decode ( $asiakastieto->getTavoitteet() ), PDO::PARAM_STR );
  136. $stmt->bindValue ( ":kokemus", utf8_decode ( $asiakastieto->getKokemus() ), PDO::PARAM_STR );
  137. $stmt->bindValue ( ":taajuus", utf8_decode ( $asiakastieto->getTaajuus() ), PDO::PARAM_STR );
  138. $stmt->bindValue ( ":vammat", utf8_decode ( $asiakastieto->getVammat() ), PDO::PARAM_STR );
  139.  
  140. // Jotta id:n saa lisäyksestä, täytyy laittaa tapahtumankäsittely päälle
  141. $this->db->beginTransaction();
  142.  
  143. // Suoritetaan SQL-lause (insert)
  144. if (! $stmt->execute ()) {
  145. $virhe = $stmt->errorInfo ();
  146.  
  147. if ($virhe [0] == "HY093") {
  148. $virhe [2] = "Invalid parameter";
  149. }
  150.  
  151. // Perutaan tapahtuma
  152. $this->db->rollBack();
  153.  
  154. throw new PDOException ( $virhe [2], $virhe [1] );
  155. }
  156.  
  157. // id täytyy ottaa talteen ennen tapahtuman päättymistä
  158. $id = $this->db->lastInsertId ();
  159.  
  160. $this->db->commit();
  161.  
  162. // Palautetaan lisätyn ilmoituksen id
  163. return $id;
  164. }
  165.  
  166. public function poistaAsiakas($id) {
  167. $sql = "DELETE FROM asiakas WHERE id = :id";
  168.  
  169. // Valmistellaan lause, prepare on PDO-luokan metodeja
  170. if (! $stmt = $this->db->prepare ( $sql )) {
  171. $virhe = $this->db->errorInfo ();
  172. throw new PDOException ( $virhe [2], $virhe [1] );
  173. }
  174.  
  175. // Sidotaan parametrit
  176. $stmt->bindValue ( ":id", $id, PDO::PARAM_INT );
  177.  
  178. // Ajetaan lauseke
  179. if (! $stmt->execute ()) {
  180. $virhe = $stmt->errorInfo ();
  181.  
  182. if ($virhe [0] == "HY093") {
  183. $virhe [2] = "Invalid parameter";
  184. }
  185.  
  186. throw new PDOException ( $virhe [2], $virhe [1] );
  187. }
  188. }
  189.  
  190. public function ajaLause($sql){
  191. $tulos = mysql_query($sql);
  192. while($rivi=mysql_fetch_assoc($tulos)) {
  193. $resultset[] = $rivi;
  194. }
  195. if(!empty($resultset))
  196. return $resultset;
  197. }
  198.  
  199. }
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement