Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Nie chce mi wyswietlic danych z bazy
Rafiks1992
post
Post #1





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 27.06.2017

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


Witam! To moj pierwszy post, zaczynam uczyc sie php ogladalem troche filmikow i zaczalem tworzyc strone, wlasciwie to moze byc nawet cos podobnego do gry via www w celach nauki lecz mam pewien problem dlatego chcialem prosic tutaj o pomoc

Dokladniej chodzi mi o to ze nie chce wyswietlic mi danych z bazy, dodalem do tabeli users kolumne money i ustawilem wartosc domyslna na 3.
Teraz w pliku ktory wyswietla profil uzytkownika chce dodac opcje ktora bedzie wyswietlala aktualny stan konta uzytkownika czyli 3.

Tak wiec w pliku youraccount.php dodaje taki kod:
  1. <?php
  2.  
  3. $money = 'SELECT money FROM users';
  4. echo 'Twoja ilosc pieniedzy to:'.$money;
  5.  
  6. ?>

Jednak gdy zaktualizuje plik i odswieze strone to pokazuje mi poprostu moje zapytanie czyli:

Twoja ilosc pieniedzy to: SELECT money FROM users

Moglby ktos mnie nakierowac jak to poprawic? Szukalem w internecie kilka sposobow i probowalem tez w innych formatach to zrobic ale nic mi nie pomoglo. Mam nadzieje ze nie zostane na dzien dobry zlinczowany za to ale kazdy kiedys zaczynal (IMG:style_emoticons/default/wink.gif) !

Ten post edytował Kshyhoo 28.06.2017, 08:59:16
Powód edycji: [Kshyhoo]: Używaj bb-code do kodu...
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Rafiks1992
post
Post #2





Grupa: Zarejestrowani
Postów: 28
Pomógł: 0
Dołączył: 27.06.2017

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


Sprobowalem tak jak podales wyskoczyl mi blad:
Fatal error: Cannot use object of type User as array in /home/.../account.php on line 172

Linia 172:
echo $userdata['money'];


A w pliku o ktorym mowiles gdzie podaje sie klase uzytkownika znalazlem dane ktore wyswietlane sa w tabeli dokladniej takie cos:

plik userclass.php
  1. <?php
  2.  
  3. class User {
  4.  
  5. protected static $table_name = "users";
  6. protected static $db_fields = array('id', 'username', 'password', 'email', 'status', 'actcode' ,'created_at' ,'money');
  7.  
  8. public $id;
  9. public $username;
  10. public $password;
  11. public $email;
  12. public $status = "pending";
  13. public $actcode;
  14. public $created_at;
  15. public $money;
  16.  
  17. public static function authenticate($username = "", $password = "") {
  18. global $database;
  19.  
  20. $username = $database->escape_value($username);
  21. $password = $database->escape_value($password);
  22. $password = secure_string($password);
  23.  
  24. $sql = "SELECT * FROM " . self::$table_name . " users WHERE username = '{$username}' AND password = '{$password}' LIMIT 1";
  25. $result_array = self::find_by_sql($sql);
  26. return !empty($result_array) ? array_shift($result_array) : false;
  27. }
  28.  
  29. public static function activate($username, $actcode) {
  30. global $database;
  31. global $session;
  32.  
  33. $username = $database->escape_value($username);
  34. $actcode = $database->escape_value($actcode);
  35.  
  36. $sql = "SELECT status FROM users WHERE username = '{$username}' AND actcode = '{$actcode}' AND status = 'pending' LIMIT 1";
  37. $database->query($sql);
  38. if($database->affected_rows() == 1) {
  39. $sql = "UPDATE users SET status = 'confirmed' WHERE username = '{$username}' AND actcode = '{$actcode}'";
  40. $result = $database->query($sql);
  41.  
  42. if($result) {
  43. return true;
  44. } else {
  45. return false;
  46. }} else {
  47. return false;
  48. }
  49. }
  50.  
  51. public static function getUser($username) {
  52. global $database;
  53.  
  54. $username = $database->escape_value($username);
  55.  
  56. $sql = "SELECT * FROM users WHERE username = '{$username}' LIMIT 1";
  57. $result_array = self::find_by_sql($sql);
  58. return !empty($result_array) ? array_shift($result_array) : false;
  59.  
  60. }
  61.  
  62. public static function user_email($username, $email) {
  63. global $database;
  64.  
  65. $username = $database->escape_value($username);
  66. $email = $database->escape_value($email);
  67.  
  68. $sql = "SELECT username FROM users WHERE username = '{$username}' AND email = '{$email}' LIMIT 1";
  69. $database->query($sql);
  70. if($database->affected_rows() == 1) {
  71. return true;
  72. } else
  73. return false;
  74. }
  75.  
  76. public static function exists($where,$username) {
  77. global $database;
  78.  
  79. $username = $database->escape_value($username);
  80. $where = $database->escape_value($where);
  81.  
  82. $sql = "SELECT '{$where}' FROM users WHERE {$where} = '{$username}' LIMIT 1";
  83. $database->query($sql);
  84. if($database->affected_rows() == 1) {
  85. return true;
  86. } else
  87. return false;
  88. }
  89.  
  90.  
  91. public static function find_all() {
  92. return self::find_by_sql("SELECT * FROM " . self::$table_name);
  93. }
  94.  
  95. public static function find_by_id($id = 0) {
  96. $result_array = self::find_by_sql("SELECT * FROM " . self::$table_name . " WHERE id={$id} LIMIT 1");
  97. return !empty($result_array) ? array_shift($result_array) : false;
  98. }
  99.  
  100. public static function find_by_sql($sql = "") {
  101. global $database;
  102. $result_set = $database->query($sql);
  103. $object_array = array();
  104. while($row = $database->fetch_array($result_set)) {
  105. $object_array[] = self::instantiate($row);
  106. }
  107. return $object_array;
  108. }
  109.  
  110. public static function count_all() {
  111. global $database;
  112. $sql = "SELECT COUNT(*) FROM " . self::$table_name;
  113. $result_set = $database->query($sql);
  114. $row = $database->fetch_array($result_set);
  115. return array_shift($row);
  116. }
  117.  
  118. private static function instantiate($record) {
  119. $object = new self;
  120. foreach($record as $attribute => $value) {
  121. if($object->has_attribute($attribute)) {
  122. $object->$attribute = $value;
  123. }
  124. }
  125. return $object;
  126. }
  127.  
  128. private function has_attribute($attribute) {
  129. return array_key_exists($attribute, $this->attributes());
  130. }
  131.  
  132. protected function attributes() {
  133. $attributes = array();
  134. foreach(self::$db_fields as $field) {
  135. if(property_exists($this, $field)) {
  136. $attributes[$field] = $this->$field;
  137. }
  138. }
  139. return $attributes;
  140. }
  141.  
  142. protected function sanitized_attributes() {
  143. global $database;
  144. $clean_attributes = array();
  145. foreach($this->attributes() as $key => $value) {
  146. $clean_attributes[$key] = $database->escape_value($value);
  147. }
  148. return $clean_attributes;
  149. }
  150.  
  151. public function create() {
  152. global $database;
  153. $attributes = $this->sanitized_attributes();
  154. $sql = "INSERT INTO " . self::$table_name . " (";
  155. $sql .= join(", ", array_keys($attributes));
  156. $sql .= ") VALUES ('";
  157. $sql .= join("', '", array_values($attributes));
  158. $sql .= "')";
  159. if($database->query($sql)) {
  160. $this->id = $database->insert_id();
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166.  
  167. public function update() {
  168. global $database;
  169. $attributes = $this->sanitized_attributes();
  170. $attribute_pairs = array();
  171. foreach($attributes as $key => $value) {
  172. $attribute_pairs[] = "{$key}='{$value}'";
  173. }
  174. $sql = "UPDATE " . self::$table_name . " SET ";
  175. $sql .= join(", ", $attribute_pairs);
  176. $sql .= " WHERE id=" . $database->escape_value($this->id);
  177. $database->query($sql);
  178. return ($database->affected_rows() == 1) ? true : false;
  179. }
  180.  
  181. public function delete() {
  182. global $database;
  183. $sql = "DELETE FROM " . self::$table_name;
  184. $sql .= " WHERE id=" . $database->escape_value($this->id);
  185. $sql .= " LIMIT 1";
  186. $database->query($sql);
  187. return ($database->affected_rows() == 1) ? true : false;
  188. }
  189.  
  190. }
  191.  
  192. ?>



Dodalem tam na koncu money oraz public $money ale i tak nic nie pomoglo gdzies jeszcze tego mi brakuje (IMG:style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post

Posty w temacie
- Rafiks1992   [PHP] Nie chce mi wyswietlic danych z bazy   28.06.2017, 08:51:49
- - viking   Wyświetlasz teraz string. Musisz jeszcze to zapyta...   28.06.2017, 08:54:57
- - Pyton_000   Oglądałeś filmiki... Albo nie doszedłeś jeszcze do...   28.06.2017, 09:02:59
- - Rafiks1992   Wporzadku bede uzywal bbcode jesli bedzie jakis ko...   28.06.2017, 09:36:43
- - viking   Podałem ci link gdzie masz wszystko opisane. Szybk...   28.06.2017, 09:38:15
- - Rafiks1992   Wybacz kolego Viking nie zauwazylem tego PDO mysla...   28.06.2017, 10:36:43
- - Pyton_000   Bo do zapytania odbierasz zmienną z `$_PO...   28.06.2017, 10:38:38
- - Rafiks1992   A pownienem uzyc $_GET? Bo nie rozumiem teraz...   28.06.2017, 11:18:02
- - nospor   Miedzy uszami masz taki malutki guzik. Wlacz go, o...   28.06.2017, 11:32:37
- - viking   Wszystko zależy skąd pochodzi ta zmienna. form met...   28.06.2017, 11:32:52
- - Rafiks1992   No tak jedna dobra wiadomosc ktora dobrze nakieruj...   29.06.2017, 10:18:31
- - nospor   Wcisnij ten guzik do konca.... Chcesz pobrac MO...   29.06.2017, 10:33:27
- - Rafiks1992   Zanim guzik do konca wcisniety bedzie to troszke m...   29.06.2017, 10:58:58
- - nospor   $userdata = User::getUser($session->u...   29.06.2017, 11:02:47
- - Rafiks1992   Sprobowalem tak jak podales wyskoczyl mi blad: Fat...   29.06.2017, 11:08:50
- - nospor   Nie: echo $userdata['money']; a: ec...   29.06.2017, 11:12:31
- - Rafiks1992   Oczywiscie zmienilem to i ogolnie wyglada to tak: ...   29.06.2017, 11:15:24
- - nospor   Powinno dzialac teraz. Jestes pewien ze ten user m...   29.06.2017, 11:19:13
- - Rafiks1992   Jak najbadziej ustawilem w phpmyadmin w money zeby...   29.06.2017, 11:21:55
- - viking   var_dump($userdata);   29.06.2017, 11:25:28
- - Rafiks1992   Kolega pomysl dobry ma bo wklepalem pomiedzy tymi ...   29.06.2017, 11:28:11
- - nospor   Wyglada jakbys edytowal zly plik user przez co zmi...   29.06.2017, 11:35:16
- - viking   A gdzie dodałeś kolumnę? Na której pozycji w bazie...   29.06.2017, 11:36:55
- - Rafiks1992   Kolumne dodalem na samym koncu w tabeli pozycje ma...   29.06.2017, 11:39:32
- - viking   To jeszcze w linii 57 var_dump($result_array)...   29.06.2017, 11:52:24
- - Rafiks1992   W ktorym kodzie masz na mysli linijke 57 ? w accou...   29.06.2017, 11:53:38
- - nospor   No wywal ten var_dump to sie pozbedziesz go.   29.06.2017, 12:00:00
- - Rafiks1992   Ehh naprawde moja glupota by Was zadziwila czasami...   29.06.2017, 12:04:37
- - nospor   CytatNigdy nie wpadlbym na tego var_dumpaPrzeciez ...   29.06.2017, 12:08:31


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: 16.10.2025 - 13:45