Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php
  2.  
  3. $host = "localhost";
  4. $db = "dev";
  5. $user = "dev";
  6. $pass = "dev";
  7. $dsn = "mysql:host=$host;dbname=$db;";
  8.  
  9. try
  10. {
  11. $pdo = new PDO($dsn, $user, $pass);
  12. } catch (PDOException $ex) {
  13. echo 'Connection failed: ' . $ex->getMessage();
  14. die();
  15. }
  16.  
  17. ?>
  18.  
  19. <?php
  20.  
  21. include_once('pdo.inc.php');
  22.  
  23. class Customers
  24. {
  25. function GetCustomerName($pdo, $customer_id)
  26. {
  27. $query = "SELECT first_name FROM Customers WHERE customer_id = :customer_id LIMIT 1";
  28. $stmt = $pdo->prepare($query);
  29. $stmt->bindParam(':customer_id', $customer_id);
  30. $stmt->execute();
  31. return $stmt->fetchColumn();
  32. }
  33. }
  34.  
  35. ?>
  36.  
  37. <?php
  38.  
  39. include_once('customers.class.php');
  40.  
  41. $a = new Customers();
  42.  
  43. echo $a->GetCustomerName($pdo, 1);
  44.  
  45. ?>
  46.  
  47. function GetCustomerName(PDO $pdo, $customer_id) {
  48.  
  49. function GetCustomerName(PDO $pdo, int $customer_id) {
  50.  
  51. if (empty($customer_id) || !is_int($customer_id) || $customer_id < 1) {
  52. throw new InvalidArgumentException(
  53. 'Send me a valid positive integer for $customer_id' .
  54. ' Value sent: ' . var_export($customer_id, true)
  55. );
  56. }
  57.  
  58. <?php
  59. define('DB_CONFIG_HOST', 'localhost');
  60. define('DB_CONFIG_DB', 'dev');
  61. define('DB_CONFIG_USER', 'dev');
  62. define('DB_CONFIG_PW', 'dev');
  63. $dsn = 'mysql:host=' . DB_CONFIG_HOST . ';dbname=' . DB_CONFIG_DB . ';';
  64. define('DB_CONFIG_DSN', $dsn);l
  65. ?>
  66.  
  67. <?php
  68. class Customer
  69. {
  70. function getCustomerName(PDO $pdo, $customer_id)
  71. {
  72. // validate parameters
  73. // in PHP 7 you would just add int type hinting in method signature
  74. // and could change the conditional below to only look
  75. // positive integer values assuming the id is > 0
  76. if (empty($customer_id) || !is_int($customer_id) || $customer_id < 1) {
  77. throw new InvalidArgumentException(
  78. 'Send me a valid positive integer for $customer_id' .
  79. ' Value sent: ' . var_export($customer_id, true)
  80. );
  81. }
  82.  
  83. // query DB for customer name
  84. $query = "SELECT first_name
  85. FROM Customers
  86. WHERE customer_id = :customer_id
  87. LIMIT 1";
  88. try {
  89. $stmt = $pdo->prepare($query);
  90. $stmt->bindParam(':customer_id', $customer_id);
  91. $stmt->execute();
  92. if($stmt->rowCount === 0) {
  93. // no match found
  94. return false;
  95. } else {
  96. return $stmt->fetchColumn();
  97. }
  98. } catch (PDOException $e) {
  99. error_log(
  100. 'Something went wrong with query. Message: ' .
  101. $e->getMessage();
  102. );
  103. // you could also rethrow exception here instead of returning null
  104. // if you want to abort method completion and have caller handle exception.
  105. // or of course not use try-catch block here at all
  106. // if you don't want any extra exception handling in this method.
  107. return null;
  108. }
  109. }
  110. }
  111.  
  112. ?>
  113.  
  114. <?php
  115.  
  116.  
  117. include_once('pdoConfig.inc.php');
  118. include_once('customers.class.php');
  119.  
  120. // instantiate PDO object
  121. try
  122. {
  123. $pdo = new PDO(DB_CONFIG_DSN, DB_CONFIG_USER, DB_CONFIG_PW);
  124. } catch (PDOException $e) {
  125. error_log('Connection failed with: ' . $e->getMessage());
  126. die();
  127. }
  128.  
  129. // instantiate customer object
  130. // you might want to include in try-catch if instantiation is expected
  131. // to potentially throw exceptions
  132. $a = new Customer();
  133.  
  134. try {
  135. echo $a->getCustomerName($pdo, 1);
  136. } catch (Exception $e) {
  137. error_log('Customer name lookup failed with: ' . $e->getMessage());
  138. }
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement