Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Błąd w skrypcie z książki
Forum PHP.pl > Forum > Przedszkole
HaMtAr0
Witam!
Uczę się programowania w php, mam książkę "Php i Mysql - aplikacje bazozadaniowe", razem z książką był zamieszczony kod który znajduje się w książce..
Jednak po jego odpaleniu występuje błąd:
  1. <?php
  2. Catchable fatal error: Object of class winestoreTemplate could not be converted to string in /home/adam/public_html/workspace/przyklady_ksiazki/wda2-winestore/includes/customHandler.inc on line 44
  3. ?>

Cały plik:
  1. <?php
  2. // Source code example for Web Database Applications with PHP and MySQL, 2nd Editi
    on
  3. // Author: Hugh E. Williams, 2001-3
  4. // 
  5. // Unless otherwise stated, the source code distributed with this book can be
  6. // redistributed in source or binary form so long as an acknowledgment appears
  7. // in derived source files.
  8. // The citation should list that the code comes from Hugh E. Williams and David 
  9. // Lane, "Web Database Application with PHP and MySQL" published by O'Reilly & 
  10. // Associates.
  11. //
  12. // This code is under copyright and cannot be included in any other book,
  13. // publication, or educational product without permission from O'Reilly &
  14. // Associates. No warranty is attached; we cannot take responsibility for errors 
  15. // or fitness for use.
  16.  
  17. require_once "winestore.inc";
  18.  
  19. // Back trace an error
  20. function backTrace($context)
  21. {
  22.  $calls = "";
  23.  
  24.  // Get a backtrace of the function calls
  25.  $trace = debug_backtrace();
  26.  
  27.  $calls = "\nBacktrace:";
  28.  
  29.  // Start at 2 -- ignore this function (0) and the customHandler() (1)
  30.  for($x=2; $x < count($trace); $x++)
  31.  {
  32.  $callNo = $x - 2;
  33.  $calls .= "\n {$callNo}: {$trace[$x]["function"]} ";
  34.  $calls .= "(line {$trace[$x]["line"]} in {$trace[$x]["file"]})";
  35.  }
  36.  
  37.  $calls .= "\nVariables in {$trace[2]["function"]} ():";
  38.  
  39.  // Use the $context to get variable information for the function
  40.  // with the error
  41.  foreach($context as $name => $value)
  42.  {
  43.  if (!empty($value))
  44.  $calls .= "\n {$name} is {$value}";
  45.  else
  46.  $calls .= "\n {$name} is NULL";
  47.  }
  48.  return ($calls);
  49. }
  50. // Custom error handler function -- reproduced from Chapter 12
  51. function customHandler($number, $string, $file, $line, $context)
  52. {
  53. $error = "";
  54.  
  55. switch ($number)
  56. {
  57.  case E_USER_ERROR:
  58.  $error .= "\nERROR on line {$line} in {$file}.\n";
  59.  $stop = true;
  60.  break;
  61.  case E_WARNING:
  62.  case E_USER_WARNING:
  63.  $error .= "\nWARNING on line {$line} in {$file}.\n";
  64.  $stop = false;
  65.  break;
  66.  case E_NOTICE:
  67.  case E_USER_NOTICE:
  68.  $error .= "\nNOTICE on line {$line} in {$file}.\n";
  69.  $stop = false;
  70.  break;
  71.  default:
  72.  $error .= "UNHANDLED ERROR on line {$line} in {$file}.\n";
  73.  $stop = false;
  74. }
  75. $error .= "Error: \"{$string}\" (error #{$number}).";
  76. $error .= backTrace($context);
  77. $error .= "\nClient IP: {$_SERVER["REMOTE_ADDR"]}";
  78.  
  79. $prepend = "\n[PHP Error " . date("YmdHis") . "]";
  80. $error = ereg_replace("\n", $prepend, $error);
  81.  
  82. if (SCREEN_ERRORS)
  83.  print "<pre>{$error}</pre>";
  84.  
  85. if (FILE_ERRORS)
  86.  error_log($error, 3, ERROR_FILE);
  87.  
  88. if ($stop == true)
  89. {
  90. if (isset($_SESSION["order_no"]))
  91.  unset($_SESSION["order_no"]);
  92.  
  93.  if (isset($_SESSION["loginUsername"]))
  94. unset($_SESSION["loginUsername"]);
  95.  
  96.  if (isset($_SESSION["loginIP"]))
  97. unset($_SESSION["loginIP"]);
  98.  
  99. die(); 
  100. }
  101. }
  102.  
  103. ?>

Interesuje mnie czy błąd dotyczy jednej pętli? Czy cały skrypt jest niekompatybilny z nową wersją php?
Cysiaczek
Przyznam, że nie bardzo widzę błąd o którym mówisz. Możesz pokazać i zaznaczyć komentarzem dokładnie tą linię, która wywołuje błąd? W 44 lini jest $calls, która od poczatku jest stringiem, więc nie jej dotyczy problem. Tak, to może byc wina php, napisz jaką masz wersję.

btw. Popraw proszę tytuł topiku na zgodny z zasadami forum Przedszkole.
Jak dostarczysz więcej informacji, to może przeniosę na PHP

Pozdrawiam.

--edit
Zamykam - brak poprawionego tematu - kontakt na PW
HaMtAr0
Cytat(Cysiaczek @ 11.08.2007, 17:14:18 ) *
Przyznam, że nie bardzo widzę błąd o którym mówisz. Możesz pokazać i zaznaczyć komentarzem dokładnie tą linię, która wywołuje błąd? W 44 lini jest $calls, która od poczatku jest stringiem, więc nie jej dotyczy problem. Tak, to może byc wina php, napisz jaką masz wersję.

btw. Popraw proszę tytuł topiku na zgodny z zasadami forum Przedszkole.
Jak dostarczysz więcej informacji, to może przeniosę na PHP

Pozdrawiam.

--edit
Zamykam - brak poprawionego tematu - kontakt na PW

Moja wersja php to: php-5.2.4_pre200708051230-r2
Tak, problem dotyczy lini 44, wkleiłem cały plik, chyba że nie liczy się pustych linii czy komentarzy?
Cysiaczek
Przeciez to nie ten plik...
Pokaż winestore.inc , który includujesz na początku... ;/
btw - o takiej wersji php nie słyszałem...
HaMtAr0
Cytat(Cysiaczek @ 12.08.2007, 17:06:34 ) *
Przeciez to nie ten plik...
Pokaż winestore.inc , który includujesz na początku... ;/
btw - o takiej wersji php nie słyszałem...

Ogólnie cały skrypt znajduje się na stronie http://www.webdatabasebook.com/
Wersja php z portage, bo używam distro Gentoo, w razie potrzeby mogę zainstalować starszą wersję php..
Zamieściłem poprawny plik tongue.gif, ale jak chcesz - Proszę plik winestore.inc
  1. <?php
  2. require_once 'db.inc';
  3. require_once 'customHandler.inc';
  4.  
  5. // Choose or adjust one of the following
  6. // NOTE: do not add a trailing slash
  7. // define("D_INSTALL_PATH", "c:/progra~1/easyph~1/www");
  8. // define("D_INSTALL_PATH", "/Library/WebServer/Documents");
  9. define("D_INSTALL_PATH", "/home/adam/public_html/workspace/przyklady_ksiazki");
  10.  
  11. // Paths -- for these, add trailing slash
  12. define("D_WEB_PATH", "/wda2-winestore/");
  13. define("D_CART", D_WEB_PATH . "cart/");
  14. define("D_CARTIMAGES", D_CART . "images/");
  15. define("D_CUSTOMER", D_WEB_PATH . "customer/");
  16. define("D_AUTH", D_WEB_PATH . "auth/");
  17. define("D_ORDER", D_WEB_PATH . "order/");
  18. define("D_SEARCH", D_WEB_PATH . "search/");
  19. define("D_TEMPLATES", D_INSTALL_PATH . D_WEB_PATH . "templates/");
  20.  
  21. // No slash at beginning
  22. // S - scripts
  23. define("S_MAIN", D_WEB_PATH . "index.php");
  24. define("S_ADDTOCART", D_CART . "addtocart.php");
  25. define("S_EMPTYCART", D_CART . "emptycart.php");
  26. define("S_SHOWCART", D_CART . "showcart.php");
  27. define("S_UPDATECART", D_CART . "updatecart.php");
  28. define("S_ORDER_1", D_ORDER . "order-step1.php");
  29. define("S_ORDER_2", D_ORDER . "order-step2.php");
  30. define("S_ORDER_3", D_ORDER . "order-step3.php");
  31. define("S_ORDER_4", D_ORDER . "order-step4.php");
  32. define("S_ORDERRECEIPT", D_ORDER . "receipt.php");
  33. define("S_SEARCH", D_SEARCH . "search.php");
  34. define("S_SEARCHFORM", D_SEARCH . "searchform.php");
  35. define("S_DETAILS", D_CUSTOMER . "details.php");
  36. define("S_VALIDATE", D_CUSTOMER . "validate.php");
  37. define("S_CUSTRECEIPT", D_CUSTOMER . "receipt.php");
  38. define("S_LOGOUT", D_AUTH . "logout.php");
  39. define("S_LOGIN", D_AUTH . "login.php");
  40. define("S_LOGINCHECK", D_AUTH . "logincheck.php");
  41. define("S_PASSWORD", D_AUTH . "password.php");
  42. define("S_CHANGEPASSWORD", D_AUTH . "changepassword.php");
  43. define("S_PASSWORDRECEIPT", D_AUTH . "receipt.php");
  44.  
  45. // T - templates
  46. define("T_SKELETON", "winestore.tpl");
  47. define("T_HOME", "index.tpl");
  48. define("T_SHOWCART", "showcart.tpl");
  49. define("T_DETAILS", "details.tpl");
  50. define("T_CUSTRECEIPT", "custreceipt.tpl");
  51. define("T_LOGIN", "login.tpl");
  52. define("T_PASSWORD", "password.tpl");
  53. define("T_PASSWORDRECEIPT", "passwordreceipt.tpl");
  54. define("T_EMAIL", "email.tpl");
  55. define("T_ORDERRECEIPT", "orderreceipt.tpl");
  56. define("T_SEARCH", "search.tpl");
  57. define("T_SOURCE", "source.tpl");
  58.  
  59. // I - images
  60. define("I_CART_OFF", D_CARTIMAGES . "cart_off.jpg");
  61. define("I_CART_ON", D_CARTIMAGES . "cart_on.jpg");
  62.  
  63. // B - Buttons
  64. define("B_EMPTY_CART", 1);
  65. define("B_SHOW_CART", 2);
  66. define("B_UPDATE_CART", 4);
  67. define("B_PURCHASE", 8);
  68. define("B_SEARCH", 16);
  69. define("B_HOME", 32);
  70. define("B_DETAILS", 64);
  71. define("B_LOGINLOGOUT", 128);
  72. define("B_PASSWORD", 256);
  73. define("B_ALL", 511);
  74.  
  75. // Show the cart icon?
  76. define("NO_CART", 1);
  77. define("SHOW_ALL", 2);
  78.  
  79. // Search rows per page
  80. define("SEARCH_ROWS", 12);
  81.  
  82. // Custom error handler controls
  83. // File to log errors to
  84. define("ERROR_FILE", "/tmp/php_error_log");
  85.  
  86. // Save errors to a file?
  87. define("FILE_ERRORS", false);
  88.  
  89. // Show errors to the screen?
  90. define("SCREEN_ERRORS", true);
  91.  
  92. // The database connection string
  93. $dsn = "mysql://{$username}:{$password}@{$hostname}/{$databasename}";
  94.  
  95. // Untaint user data
  96. function pearclean($array, $index, $maxlength, $connection)
  97. {
  98. if (isset($array["{$index}"]))
  99. {
  100. $input = trim(substr($array["{$index}"], 0, $maxlength));
  101. $input = mysql_real_escape_string($input);
  102. return ($input);
  103. }
  104. return NULL;
  105. }
  106.  
  107. // Find the cust_id using the user_name
  108. function getCust_id($user_name, $connection = null)
  109. {
  110.  global $dsn;
  111.  
  112.  // If a connection parameter is not passed, then 
  113.  // use our own connection
  114.  if (!isset($connection))
  115.  {
  116. $connection = DB::connect($dsn, false);
  117. if (DB::isError($connection))
  118. trigger_error($connection->getMessage(), E_USER_ERROR); 
  119.  }
  120.  
  121.  $query = "SELECT cust_id FROM users WHERE 
  122.  user_name = '{$user_name}'";
  123.  
  124.  $result = $connection->query($query);
  125.  if (DB::isError($result))
  126. trigger_error($result->getMessage(), E_USER_ERROR); 
  127.  
  128.  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  129.  
  130.  return($row["cust_id"]);
  131. }
  132.  
  133. // Show the user the details of one wine in their cart
  134. function showWine($wineId, $connection = null)
  135. {
  136.  global $dsn;
  137.  
  138.  $wineQuery = "SELECT year, winery_name, wine_name
  139.  FROM winery, wine
  140.  WHERE wine.winery_id = winery.winery_id
  141.  AND wine.wine_id = {$wineId}";
  142.  
  143.  // If a connection parameter is not passed, then 
  144.  // use our own connection to avoid any locking problems
  145.  if (!isset($connection))
  146.  {
  147. $connection = DB::connect($dsn, false);
  148. if (DB::isError($connection))
  149. trigger_error($connection->getMessage(), E_USER_ERROR); 
  150.  }
  151.  
  152.  $result = $connection->query($wineQuery);
  153.  
  154.  if (DB::isError($result))
  155. trigger_error($result->getMessage(), E_USER_ERROR); 
  156.  
  157.  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  158.  
  159.  // Print the wine details
  160.  $output = "{$row["year"]} {$row["winery_name"]} {$row["wine_name"]}";
  161.  
  162.  // Print the varieties for this wine
  163.  $output .= showVarieties($connection, $wineId);  
  164.  
  165.  return $output;
  166. }
  167.  
  168.  
  169. // Find the varieties for a wineID
  170. function showVarieties($connection, $wineID)
  171. {
  172.  // Find the varieties of the current wine,
  173.  // and order them by id
  174.  $query = "SELECT gv.variety
  175.  FROM grape_variety gv, wine_variety wv, wine w
  176.  WHERE w.wine_id = wv.wine_id
  177.  AND wv.variety_id = gv.variety_id
  178.  AND w.wine_id = {$wineID}
  179.  ORDER BY wv.id";
  180.  
  181.  $result = $connection->query($query);
  182.  
  183.  if (DB::isError($result))
  184. trigger_error($result->getMessage(), E_USER_ERROR); 
  185.  
  186.  $varieties = "";
  187.  
  188.  // Retrieve and print the varieties
  189.  while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
  190. $varieties .= " {$row["variety"]}";
  191.  
  192.  return $varieties;
  193. }
  194.  
  195. // Find the cheapest bottle price for a wineID
  196. function showPricing($connection, $wineID)
  197. {
  198.  // Find the price of the cheapest inventory
  199.  $query = "SELECT min(cost) FROM inventory
  200.  WHERE wine_id = {$wineID}";
  201.  
  202.  $result = $connection->query($query);
  203.  
  204.  if (DB::isError($result))
  205. trigger_error($result->getMessage(), E_USER_ERROR); 
  206.  
  207.  // Retrieve the oldest price
  208.  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  209.  
  210.  $price = $row["min(cost)"];
  211.  
  212.  return $price;
  213. }
  214.  
  215. // Lookup the country_id in the countries lookup table
  216. // and return the country name
  217. function showCountry($country_id, $connection)
  218. {
  219. $query = "SELECT country FROM countries WHERE 
  220. country_id = {$country_id}";
  221.  
  222. $result = $connection->query($query);
  223.  
  224. if (DB::isError($result))
  225.  trigger_error($result->getMessage(), E_USER_ERROR); 
  226.  
  227. $countryRow = $result->fetchRow(DB_FETCHMODE_ASSOC);
  228.  
  229. return($countryRow["country"]);
  230. }
  231.  
  232. // Lookup the title in the titles lookup table
  233. // and return the title string
  234. function showTitle($title_id, $connection)
  235. {
  236. $query = "SELECT title FROM titles WHERE 
  237. title_id = {$title_id}";
  238.  
  239. $result = $connection->query($query);
  240.  
  241. if (DB::isError($result))
  242.  trigger_error($result->getMessage(), E_USER_ERROR); 
  243.  
  244. $titleRow = $result->fetchRow(DB_FETCHMODE_ASSOC);
  245.  
  246. return($titleRow["title"]);
  247. }
  248.  
  249. ?>
PiotrLegnica
Chyba już wiem o co chodzi. Kod jest napisany pod PHP4, customHandler to handler błędów PHP, i dostaje kontekst jako ostatnią zmienną. A jako, że w kontekście jest obiekt winestoreTemplate, to PHP5 się burzy, bo nie wie jak go ma zmienić na string. Spróbuj dodać jakąkolwiek metodę __toString do tej klasy.
HaMtAr0
Cytat(PiotrLegnica @ 12.08.2007, 21:30:45 ) *
Chyba już wiem o co chodzi. Kod jest napisany pod PHP4, customHandler to handler błędów PHP, i dostaje kontekst jako ostatnią zmienną. A jako, że w kontekście jest obiekt winestoreTemplate, to PHP5 się burzy, bo nie wie jak go ma zmienić na string. Spróbuj dodać jakąkolwiek metodę __toString do tej klasy.

Zainstalowałem sobie na komputerze PHP w wersji 4.6, ale problem występuje ponownie ten sam :-/
Jestem zielony, i nie za bardzo wiem o co chodzi z tą metodą. Mógłbyś mi napisać coś więcej? Kod już napiszę sobie sam.
Dzięki za pomoc!
PiotrLegnica
http://pl2.php.net/manual/en/language.oop5.magic.php
A jak jest z PHP4 to nie wiem.
HaMtAr0
Cytat(PiotrLegnica @ 13.08.2007, 12:52:59 ) *

Przyznam się nie wiem jak to naprawić :-/
W tym pliku nie ma żadnej klasy..
Pozdrawiam i dziękuję za pomoc!
[edit]
W tym pliku jest definiowana klasa, http://phpfi.com/256497
Teraz to się kompletnie pogubiłem co gdzie mam zmienić :-/
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.