Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][MYSQL] Singleton zadanie
fantek
post
Post #1





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 9.10.2010

Ostrzeżenie: (0%)
-----


Witam . Mam takie zadanie:

  1. <?php
  2.  
  3. /*
  4.  
  5.   Task 2:
  6.   Implement db handler
  7.  
  8.   Create database `adb2` and user `root` with pasword `` for your project;
  9.   Create file ./lib/db.php and implement in it abstract class MjDb that extends MjSingleton;
  10.   Class should use config from clas Config.
  11.  
  12.   Class MjDb should implements methods:
  13.  
  14.   Public
  15.  
  16.   > execute(string $query)
  17.   (function that execute query and return true or return false in case failure)
  18.  
  19.   > get_row(string $query)
  20.   (function that execute query and returns row from database
  21.   or return false in case failure)
  22.  
  23.   > execute_s(string $query)
  24.   (function that execute query and returns array of rows from database
  25.   or return false in case failure)
  26.  
  27. */
  28.  
  29. include('./data/config.php');
  30. include('./lib/singleton.php');
  31. include('./lib/db.php');
  32.  
  33. class task2 extends MjDb{
  34. public function run(){
  35. $q = "
  36. CREATE TABLE IF NOT EXISTS `a_user` (
  37. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  38. `name` varchar(50) DEFAULT NULL,
  39. `pass` varchar(50) DEFAULT NULL,
  40. `salt` varchar(50) DEFAULT NULL,
  41. `type` TINYINT DEFAULT NULL,
  42. PRIMARY KEY (`id`),
  43. UNIQUE KEY `id` (`id`)
  44. ) ENGINE=InnoDB DEFAULT CHARSET=latin2 AUTO_INCREMENT=1;";
  45.  
  46. if(!$this->execute($q)){
  47. return __LINE__;
  48. }
  49.  
  50. $q = "TRUNCATE a_user";
  51. if(!$this->execute($q)){
  52. return __LINE__;
  53. }
  54.  
  55. $q = "
  56. INSERT INTO `a_user` (
  57. `id` , `name` , `pass` , `salt`
  58. )
  59. VALUES (
  60. NULL , 'name', 'pass', 'salt'
  61. );
  62. ";
  63.  
  64. for($i=0;$i<5;$i++){
  65. if(!$this->execute($q)){
  66. return __LINE__;
  67. }
  68. }
  69.  
  70. $q = "SELECT * FROM `a_user`";
  71.  
  72. $r = $this->get_row($q);
  73. if(!$r || count($r) != 4){
  74. return __LINE__;
  75. }
  76.  
  77. $r = $this->execute_s($q);
  78.  
  79. if(!$r || count($r) != 5){
  80. return __LINE__;
  81. }
  82.  
  83. return null;
  84. }
  85.  
  86. }
  87.  
  88. $error = task2::get_instance()->run();
  89.  
  90. if($error == null){
  91. echo 'Test Completed';
  92. }else{
  93. echo 'Test Failed on line '.$error;
  94. }


Pomijając wszystkie szczegóły klas zastanawia mnie jedna rzecz:
Ten fragment kodu
  1. FOR($i=0;$i<5;$i++){
  2. IF(!$this->execute($q)){
  3. RETURN __LINE__;
  4. }
  5. }


utworzy mi w bazie 5 identycznych rekordów .

Następnie mamy:

  1. $q = "SELECT * FROM `a_user`";
  2.  
  3. $r = $this->get_row($q);
  4. if(!$r || count($r) != 4){
  5. return __LINE__;
  6. }

Która to metoda zwracać będzie ilość wierszy w bazie (skoro wcześniej utworzyło mi 5 rekordów w bazie to wywali mi error __LINE__ bo jest 5 wierszy)

  1.  
  2. $r = $this->execute_s($q);
  3.  
  4. if(!$r || count($r) != 5){
  5. return __LINE__;
  6. }


Tutaj się zgodzę bo metoda zwróci mi tablice z 5 rekordami.


Dlatego się zastanawiam czy bodajże nie ma tu haczyka jakiegoś czy to po prostu jakiś błąd ?

Go to the top of the page
+Quote Post
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Przeczytaj jeszcze raz co zwraca get_row()... przeciez masz wyraznie napisane a mimo to dodajesz jakąś wlasna teorie...
Go to the top of the page
+Quote Post
fantek
post
Post #3





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 9.10.2010

Ostrzeżenie: (0%)
-----


Nie czaje do tej pory .... o co chodzi ...
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




(IMG:style_emoticons/default/facepalmxd.gif)

Ty twierdzisz, ze get_row zwraca wszystkie wierwsze z zapytania:
Cytat
Która to metoda zwracać będzie ilość wierszy


Zas w komentarzach jak wol masz napisane, ze get_row zwraca pojedynczy wiersz z zapytania
Cytat
> get_row(string $query)

(function that execute query and returns row from database

or return false in case failure)

Jesli nadal nie widzisz roznicy to palka sie nalezy jak nic (IMG:style_emoticons/default/smile.gif)
Jesli nadal to naprawde dla Ciebie niepojete to wez sobie sprawdz co zawiera twoje $r to moze cie oswieci, jesli prosta teoria do ciebie nie przemawia
var_dump($r);
Go to the top of the page
+Quote Post
fantek
post
Post #5





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 9.10.2010

Ostrzeżenie: (0%)
-----


No ok ale i tak jak wróci pojedynczy wiersz to wyjdzie 5 bo wcześniej tworzymy 5 kolumn (`id``name``pass``salt``type`) .
Co do metody get_row($q) sam muszę napisać żeby właśnie zwracała wiersz z bazy i zrobiłem to tak:
  1. <?php
  2.  
  3. abstract class MjDb extends MjSingleton{
  4.  
  5. private $_query = null;
  6.  
  7. public function execute($q, $params = array()) {
  8.  
  9. $this->_error = false;
  10.  
  11. if($this->_query = $this->_pdo->prepare($q)) {
  12. $x = 1;
  13. if(count($params)) {
  14. foreach($params as $param) {
  15. $this->_query->bindValue($x, $param);
  16. $x++;
  17. }
  18. }
  19.  
  20. if($this->_query->execute()) {
  21. $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
  22. $this->_count = $this->_query->rowCount();
  23. } else {
  24. $this->_error = true;
  25. }
  26. }
  27.  
  28. return $this;
  29. }
  30.  
  31. public function get_row($q){
  32.  
  33. $query = $this->_query = $this->_pdo->prepare($q);
  34. $query ->execute();
  35. $row = $query->fetch(PDO::FETCH_ASSOC);
  36. return $row;
  37.  
  38.  
  39.  
  40. }
  41. }



var_dump($r)
  1. array (size=5)
  2. 'id' => string '1' (length=1)
  3. 'name' => string 'name' (length=4)
  4. 'pass' => string 'pass' (length=4)
  5. 'salt' => string 'salt' (length=4)
  6. 'type' => null



Czyli 5 (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Tak, 5 kolumn a nie 5 wierszy (IMG:style_emoticons/default/smile.gif)
Pewnie ktos sie rypnal, albo zmienil tabele user i zapomnial o zmianie liczby kolumn
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 20:11