Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [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

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 08:15