Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. database.pdo.php
  2. <?php
  3.     class DataBase
  4.     {
  5.         private $dbh; // DataBase Handler
  6.        
  7.         public function __construct($ip, $user, $password, $db, $port, $charset)
  8.         {
  9.             try
  10.             {
  11.                 $dsn = "mysql:host=$ip;port=$port;dbname=$db;charset=$charset";
  12.                 $opt = array
  13.                 (
  14.                     PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  15.                     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  16.                 );
  17.                
  18.                 $this->dbh = new PDO($dsn, $user, $password, $opt);
  19.             }
  20.             catch(PDOException $e)
  21.             {
  22.                 echo 'Произошла ошибка подключения к базе данных!<br>';
  23.                 echo $e->getMessage();
  24.                 die();
  25.             }
  26.         }
  27.        
  28.         function __destruct()
  29.         {
  30.             try
  31.             {
  32.                 $this->dbh = null; // Closes connection
  33.             }
  34.             catch (PDOException $e)
  35.             {
  36.                 echo 'Произошла ошибка заверщения работы с базой данных!<br>';
  37.                 echo $e->getMessage();
  38.                 die();
  39.             }
  40.         }
  41.        
  42.         public function getDBHandler()
  43.         {
  44.             return $this->dbh;
  45.         }
  46.     }
  47. ?>
  48.  
  49. order.class.php
  50. <?php
  51.     class Order
  52.     {
  53.         private $id = null;
  54.         private $name;
  55.         private $email;
  56.         private $cityid;
  57.         private $markid;
  58.         private $modelid;
  59.         private $catid;
  60.         private $phone;
  61.         private $sendphone;
  62.         private $imgname;
  63.         private $comment;
  64.         private $dbh;
  65.        
  66.         public function __construct($dbh, $initarray = null) // initarray is array with $name $email ...
  67.         {
  68.             if($dbh !== null)
  69.                 $this->dbh = $dbh;
  70.             else
  71.                 die("You must send DataBase handler in class Order");
  72.                
  73.             if($initarray !== null)
  74.             {
  75.                 $this->name = $initarray['name'];
  76.                 $this->email = $initarray['email'];
  77.                 $this->cityid = $initarray['cityid'];
  78.                 $this->markid = $initarray['markid'];
  79.                 $this->modelid = $initarray['modelid'];
  80.                 $this->catid = $initarray['catid'];
  81.                 $this->phone = $initarray['phone'];
  82.                 $this->sendphone = $initarray['sendphone'];
  83.                 $this->imgname = $initarray['imgname'];
  84.                 $this->comment = $initarray['comment'];
  85.             }
  86.         }
  87.        
  88.         public function getOrderById($id)
  89.         {
  90.             $this->id = $id;
  91.             $sql = "SELECT * FROM `orders` WHERE id=? ORDER BY id ASC LIMIT 1";
  92.            
  93.             try
  94.             {
  95.                 $stmt = $this->dbh->prepare($sql);
  96.                 $stmt->execute([$id]);
  97.                 $result = $stmt->fetch();
  98.             }
  99.             catch(PDOException $e)
  100.             {
  101.                 echo 'getOrderById return error:<br>';
  102.                 echo $e->getMessage();
  103.                 die();
  104.             }
  105.            
  106.             $this->name = $result['name'];
  107.             $this->email = $result['email'];
  108.             $this->cityid = $result['cityid'];
  109.             $this->markid = $result['markid'];
  110.             $this->modelid = $result['modelid'];
  111.             $this->catid = $result['catid'];
  112.             $this->phone = $result['phone'];
  113.             $this->sendphone = ($this->phone !== null);
  114.             $this->imagename = $result['imgname'];
  115.             $this->comment = $result['comment'];
  116.  
  117.             return $result; // return Assoc array
  118.         }
  119.     }
  120. ?>
  121.  
  122. show.php
  123. <?php
  124.     require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . 'config.inc.php');
  125.     require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'database.pdo.class.php');
  126.     require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'order.class.php');
  127.     $db = new DataBase($mysqlip, $mysqluser, $mysqlpassword, $db, $mysqlport, $charset);
  128.     $dbh = $db->getDBHandler();
  129. ?>
  130.  
  131. <!DOCTYPE html>
  132. <html lang="ru">
  133.     <body>
  134.         <div class="content">
  135.             <?php
  136.                     $order = new Order($dbh);
  137.                     $associnf = $order->getOrderById($id);
  138.  
  139.                     echo $associnf['name'].'<br>';
  140.                     echo $associnf['email'].'<br>';
  141.                     echo $associnf['catid'].'<br>';
  142.                     echo $associnf['markid'].'<br>';
  143.             ?>
  144.         </div>
  145.     </body>
  146. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement