Guest User

Untitled

a guest
Dec 1st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. spl_autoload_register(function($class_name) {
  2. require_once 'classes/'.$class_name.'.php';
  3. });
  4.  
  5. class DB
  6. {
  7.  
  8. private static $conn;
  9.  
  10. private static $DB_HOST = 'localhost';
  11. private static $DB_NAME = 'test';
  12. private static $DB_USER = 'root';
  13. private static $DB_PASS = '';
  14.  
  15.  
  16.  
  17.  
  18. private function __construct ()
  19. {
  20. try {
  21. $this->conn = new PDO(
  22. 'mysql:host=' . self::$DB_HOST . ';dbname=' . self::$DB_NAME,
  23. self::$DB_USER,
  24. self::$DB_PASS,
  25. [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]
  26. );
  27. echo "Успешное соединение с базой данных!";
  28.  
  29. } catch (PDOException $e) {
  30. echo "Возникла неизвестна ошибка";
  31. $now_date = new DateTime();
  32. file_put_contents('log', 'Возникла ошибка в файле ' . __FILE__ . ' в строке '
  33. . __LINE__ . PHP_EOL . 'Дата возникновения ошибки: ' . $now_date->format('d.m.Y H:i:s')
  34. . PHP_EOL . 'Текст ошибки [' . $e->getCode() . '] - ' . $e->getMessage() . PHP_EOL
  35. . str_repeat('-', 80) . PHP_EOL, FILE_APPEND);
  36. exit;
  37. }
  38. }
  39.  
  40. private function __clone () {}
  41. private function __wakeup () {}
  42.  
  43. public static function getInstance()
  44. {
  45. if (self::$conn != null) {
  46. return self::$conn;
  47. }
  48.  
  49. return new self;
  50. }
  51. }
  52.  
  53. $db = DB::getInstance();
  54.  
  55. $stmt = $conn->prepare("INSERT INTO students (name, last_name, bd)
  56. VALUES (:name, :last_name, :bd)");
  57. $stmt->bindParam(':name', $name);
  58. $stmt->bindParam(':last_name', $last_name);
  59. $stmt->bindParam(':bd', $bd);
  60.  
  61.  
  62. $name = $_POST['name'];
  63. $last_name = $_POST['last_name'];
  64. $bd = $_POST['bd'];
  65. $stmt->execute();
  66. $conn=null;
Add Comment
Please, Sign In to add comment