Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Problem z PDO
blackmilk
post 15.07.2012, 23:06:57
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 15.07.2012

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


Witam, jestem w trakcie nauki PHP i napotałem problem w jednym skrypcie przy metodzie bindParam() biblioteki PDO.
Skrypt ma za zadanie sortować dane pobrane z bazy danych według poszczególnych kolumn jednak tak się nie dzieje :/.

Kod:
  1. <?php
  2.  
  3. require_once('HTML/Table.php');
  4.  
  5.  
  6.  
  7. try {
  8.  
  9. $dbh = new PDO('mysql:host=localhost;dbname=shop', 'root', 'root');
  10.  
  11. } catch(PDOException $exception) {
  12.  
  13. echo "Błąd połączenia z bazą danych: " . $exception->getMessage() . "<br />";
  14. }
  15.  
  16.  
  17.  
  18. $table = new HTML_Table(array('border' => '1'));
  19.  
  20. @ $table->setHeaderContents(0, 0, "<a href=\"{$_SERVER['PHP_SELF']}?sort=id\">ID Zamowienia</a>");
  21. @ $table->setHeaderContents(0, 1, "<a href=\"{$_SERVER['PHP_SELF']}?sort=client_id\">ID Klienta</a>");
  22. @ $table->setHeaderContents(0, 2, "<a href=\"{$_SERVER['PHP_SELF']}?sort=order_time\">Data zamowienia</a>");
  23. @ $table->setHeaderContents(0, 3, "<a href=\"{$_SERVER['PHP_SELF']}?sort=sub_total\">Wartosc towarow</a>");
  24. @ $table->setHeaderContents(0, 4, "<a href=\"{$_SERVER['PHP_SELF']}?sort=shipping_cost\">Koszty przesylki</a>");
  25. @ $table->setHeaderContents(0, 5, "<a href=\"{$_SERVER['PHP_SELF']}?sort=total_cost\">Wartosc sumaryczna</a>");
  26.  
  27.  
  28.  
  29.  
  30.  
  31. $query = "SELECT id, client_id, order_time, sub_total, shipping_cost, total_cost FROM sales ORDER BY :sort_method";
  32.  
  33. $stmt = $dbh->prepare($query);
  34. $stmt->bindParam(':sort_method', $sort_method);
  35.  
  36. $sort_method = (isset($_GET['sort'])) ? $_GET['sort'] : 'client_id';
  37. $stmt->execute();
  38.  
  39.  
  40.  
  41.  
  42. $rownum = 1;
  43. while($row = $stmt->fetch()) {
  44.  
  45. @ $table->setCellContents($rownum, 0, $row['id']);
  46. @ $table->setCellContents($rownum, 1, $row['client_id']);
  47. @ $table->setCellContents($rownum, 2, $row['order_time']);
  48. @ $table->setCellContents($rownum, 3, $row['sub_total']);
  49. @ $table->setCellContents($rownum, 4, $row['shipping_cost']);
  50. @ $table->setCellContents($rownum, 5, $row['total_cost']);
  51.  
  52. $rownum++;
  53.  
  54. }
  55.  
  56. echo $table->toHTML();
  57.  
  58. ?>


Proszę o pomoc
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
sobol6803
post 15.07.2012, 23:14:05
Post #2





Grupa: Zarejestrowani
Postów: 115
Pomógł: 13
Dołączył: 19.06.2011
Skąd: Ruda Śląska

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


Chyba brakuje tylko tego jak ma posortować, czyli:

  1. $query = "SELECT id, client_id, order_time, sub_total, shipping_cost, total_cost FROM sales ORDER BY :sort_method ASC";

lub
  1. $query = "SELECT id, client_id, order_time, sub_total, shipping_cost, total_cost FROM sales ORDER BY :sort_method DESC";

ASC - rosnąco
DESC - malejąco

Edit:
Albo
  1. $sort_method = (isset($_GET['sort'])) ? $_GET['sort'] : 'client_id';

musi być przed
  1. $stmt->bindParam(':sort_method', $sort_method);


Ten post edytował sobol6803 15.07.2012, 23:23:40


--------------------
  1. mysql_query("DROP DATABASE $_GET['dbname']");
Go to the top of the page
+Quote Post
blackmilk
post 15.07.2012, 23:25:18
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 15.07.2012

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


Oba rozwiązania nic nie zmieniają, ciągle nie działa.
Go to the top of the page
+Quote Post
sobol6803
post 15.07.2012, 23:29:01
Post #4





Grupa: Zarejestrowani
Postów: 115
Pomógł: 13
Dołączył: 19.06.2011
Skąd: Ruda Śląska

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


Cytat(blackmilk @ 16.07.2012, 00:25:18 ) *
Oba rozwiązania nic nie zmieniają, ciągle nie działa.


A sam select w bazie działa?

  1. SELECT id, client_id, order_time, sub_total, shipping_cost, total_cost FROM sales ORDER BY client_id ASC


--------------------
  1. mysql_query("DROP DATABASE $_GET['dbname']");
Go to the top of the page
+Quote Post
blackmilk
post 15.07.2012, 23:30:34
Post #5





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 15.07.2012

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


tak, jak na sztywno podam w zapytaniu, ładnie sortuje.
Go to the top of the page
+Quote Post
d3ut3r
post 15.07.2012, 23:43:01
Post #6





Grupa: Zarejestrowani
Postów: 709
Pomógł: 176
Dołączył: 24.10.2010

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


w PDO nie możesz użyć nazw kolumn i/lub tabel jako parametrów, jedyne co Ci pozostaje to:

  1.  
  2. $sql='SELECT id, client_id, order_time, sub_total, shipping_cost, total_cost FROM sales ORDER BY '.$sort_method;
  3. $stmt = $dbh->prepare($query);
  4.  


--------------------
http://d3ut3r.wordpress.com/ | mysql_* jest przestarzałe UŻYWAJ PDO!
Go to the top of the page
+Quote Post
blackmilk
post 16.07.2012, 00:01:10
Post #7





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 15.07.2012

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


Wielkie dzięki, będzie dobrze wiedzieć na przyszłość. A z mysqli jest tak samo ?
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 20.07.2025 - 02:06