Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = "localhost";
- $db = "dev";
- $user = "dev";
- $pass = "dev";
- $dsn = "mysql:host=$host;dbname=$db;";
- try
- {
- $pdo = new PDO($dsn, $user, $pass);
- } catch (PDOException $ex) {
- echo 'Connection failed: ' . $ex->getMessage();
- die();
- }
- ?>
- <?php
- include_once('pdo.inc.php');
- class Customers
- {
- function GetCustomerName($pdo, $customer_id)
- {
- $query = "SELECT first_name FROM Customers WHERE customer_id = :customer_id LIMIT 1";
- $stmt = $pdo->prepare($query);
- $stmt->bindParam(':customer_id', $customer_id);
- $stmt->execute();
- return $stmt->fetchColumn();
- }
- }
- ?>
- <?php
- include_once('customers.class.php');
- $a = new Customers();
- echo $a->GetCustomerName($pdo, 1);
- ?>
- function GetCustomerName(PDO $pdo, $customer_id) {
- function GetCustomerName(PDO $pdo, int $customer_id) {
- if (empty($customer_id) || !is_int($customer_id) || $customer_id < 1) {
- throw new InvalidArgumentException(
- 'Send me a valid positive integer for $customer_id' .
- ' Value sent: ' . var_export($customer_id, true)
- );
- }
- <?php
- define('DB_CONFIG_HOST', 'localhost');
- define('DB_CONFIG_DB', 'dev');
- define('DB_CONFIG_USER', 'dev');
- define('DB_CONFIG_PW', 'dev');
- $dsn = 'mysql:host=' . DB_CONFIG_HOST . ';dbname=' . DB_CONFIG_DB . ';';
- define('DB_CONFIG_DSN', $dsn);l
- ?>
- <?php
- class Customer
- {
- function getCustomerName(PDO $pdo, $customer_id)
- {
- // validate parameters
- // in PHP 7 you would just add int type hinting in method signature
- // and could change the conditional below to only look
- // positive integer values assuming the id is > 0
- if (empty($customer_id) || !is_int($customer_id) || $customer_id < 1) {
- throw new InvalidArgumentException(
- 'Send me a valid positive integer for $customer_id' .
- ' Value sent: ' . var_export($customer_id, true)
- );
- }
- // query DB for customer name
- $query = "SELECT first_name
- FROM Customers
- WHERE customer_id = :customer_id
- LIMIT 1";
- try {
- $stmt = $pdo->prepare($query);
- $stmt->bindParam(':customer_id', $customer_id);
- $stmt->execute();
- if($stmt->rowCount === 0) {
- // no match found
- return false;
- } else {
- return $stmt->fetchColumn();
- }
- } catch (PDOException $e) {
- error_log(
- 'Something went wrong with query. Message: ' .
- $e->getMessage();
- );
- // you could also rethrow exception here instead of returning null
- // if you want to abort method completion and have caller handle exception.
- // or of course not use try-catch block here at all
- // if you don't want any extra exception handling in this method.
- return null;
- }
- }
- }
- ?>
- <?php
- include_once('pdoConfig.inc.php');
- include_once('customers.class.php');
- // instantiate PDO object
- try
- {
- $pdo = new PDO(DB_CONFIG_DSN, DB_CONFIG_USER, DB_CONFIG_PW);
- } catch (PDOException $e) {
- error_log('Connection failed with: ' . $e->getMessage());
- die();
- }
- // instantiate customer object
- // you might want to include in try-catch if instantiation is expected
- // to potentially throw exceptions
- $a = new Customer();
- try {
- echo $a->getCustomerName($pdo, 1);
- } catch (Exception $e) {
- error_log('Customer name lookup failed with: ' . $e->getMessage());
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement