Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- database.pdo.php
- <?php
- class DataBase
- {
- private $dbh; // DataBase Handler
- public function __construct($ip, $user, $password, $db, $port, $charset)
- {
- try
- {
- $dsn = "mysql:host=$ip;port=$port;dbname=$db;charset=$charset";
- $opt = array
- (
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- );
- $this->dbh = new PDO($dsn, $user, $password, $opt);
- }
- catch(PDOException $e)
- {
- echo 'Произошла ошибка подключения к базе данных!<br>';
- echo $e->getMessage();
- die();
- }
- }
- function __destruct()
- {
- try
- {
- $this->dbh = null; // Closes connection
- }
- catch (PDOException $e)
- {
- echo 'Произошла ошибка заверщения работы с базой данных!<br>';
- echo $e->getMessage();
- die();
- }
- }
- public function getDBHandler()
- {
- return $this->dbh;
- }
- }
- ?>
- order.class.php
- <?php
- class Order
- {
- private $id = null;
- private $name;
- private $email;
- private $cityid;
- private $markid;
- private $modelid;
- private $catid;
- private $phone;
- private $sendphone;
- private $imgname;
- private $comment;
- private $dbh;
- public function __construct($dbh, $initarray = null) // initarray is array with $name $email ...
- {
- if($dbh !== null)
- $this->dbh = $dbh;
- else
- die("You must send DataBase handler in class Order");
- if($initarray !== null)
- {
- $this->name = $initarray['name'];
- $this->email = $initarray['email'];
- $this->cityid = $initarray['cityid'];
- $this->markid = $initarray['markid'];
- $this->modelid = $initarray['modelid'];
- $this->catid = $initarray['catid'];
- $this->phone = $initarray['phone'];
- $this->sendphone = $initarray['sendphone'];
- $this->imgname = $initarray['imgname'];
- $this->comment = $initarray['comment'];
- }
- }
- public function getOrderById($id)
- {
- $this->id = $id;
- $sql = "SELECT * FROM `orders` WHERE id=? ORDER BY id ASC LIMIT 1";
- try
- {
- $stmt = $this->dbh->prepare($sql);
- $stmt->execute([$id]);
- $result = $stmt->fetch();
- }
- catch(PDOException $e)
- {
- echo 'getOrderById return error:<br>';
- echo $e->getMessage();
- die();
- }
- $this->name = $result['name'];
- $this->email = $result['email'];
- $this->cityid = $result['cityid'];
- $this->markid = $result['markid'];
- $this->modelid = $result['modelid'];
- $this->catid = $result['catid'];
- $this->phone = $result['phone'];
- $this->sendphone = ($this->phone !== null);
- $this->imagename = $result['imgname'];
- $this->comment = $result['comment'];
- return $result; // return Assoc array
- }
- }
- ?>
- show.php
- <?php
- require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . 'config.inc.php');
- require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'database.pdo.class.php');
- require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'order.class.php');
- $db = new DataBase($mysqlip, $mysqluser, $mysqlpassword, $db, $mysqlport, $charset);
- $dbh = $db->getDBHandler();
- ?>
- <!DOCTYPE html>
- <html lang="ru">
- <body>
- <div class="content">
- <?php
- $order = new Order($dbh);
- $associnf = $order->getOrderById($id);
- echo $associnf['name'].'<br>';
- echo $associnf['email'].'<br>';
- echo $associnf['catid'].'<br>';
- echo $associnf['markid'].'<br>';
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement