Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Problem z PDO
blackmilk
post
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

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: 21.08.2025 - 21:21