Load environment variables and establish database connection
This commit is contained in:
parent
6918931ecc
commit
38304ed045
|
|
@ -0,0 +1,43 @@
|
||||||
|
--
|
||||||
|
-- Create database
|
||||||
|
--
|
||||||
|
|
||||||
|
DROP DATABASE IF EXISTS `PHPStarter`;
|
||||||
|
CREATE DATABASE IF NOT EXISTS `PHPStarter`;
|
||||||
|
|
||||||
|
USE `PHPStarter`;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Create user for database
|
||||||
|
--
|
||||||
|
|
||||||
|
DROP USER IF EXISTS 'PHPStarter_user'@'localhost';
|
||||||
|
CREATE USER 'PHPStarter_user'@'localhost' IDENTIFIED BY '';
|
||||||
|
|
||||||
|
GRANT ALL PRIVILEGES ON `PHPStarter`.* TO 'PHPStarter_user'@'localhost';
|
||||||
|
FLUSH PRIVILEGES;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Drop tables
|
||||||
|
--
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `PHPStarter`;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Table structure for 'PHPStarter'
|
||||||
|
--
|
||||||
|
|
||||||
|
CREATE TABLE `PHPStarter` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`message` VARCHAR(255) NOT NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Reset 'PHPStarter'
|
||||||
|
--
|
||||||
|
|
||||||
|
DELETE FROM `PHPStarter`;
|
||||||
|
ALTER TABLE `PHPStarter` AUTO_INCREMENT = 1;
|
||||||
|
|
||||||
|
INSERT INTO `PHPStarter` (`message`) VALUES
|
||||||
|
("Welcome to PHPStarter");
|
||||||
|
|
@ -1,13 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
use Dotenv\Dotenv;
|
||||||
|
|
||||||
|
$dotenv = Dotenv::createImmutable(__DIR__ . '/../config', 'PHPStarter.env');
|
||||||
|
$dotenv->load();
|
||||||
|
|
||||||
|
use PHPStarter\DB;
|
||||||
|
|
||||||
|
$pdo = DB::connect();
|
||||||
|
|
||||||
|
$query = "SELECT * FROM `PHPStarter` WHERE `id` = 1";
|
||||||
|
$stmt = $pdo->prepare($query);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
$PHPStarter = $stmt->fetch();
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>PHPStarter</title>
|
<title><?= $PHPStarter->message ?></title>
|
||||||
<link rel="stylesheet" type="text/css" href="./css/PHPStarter.css">
|
<link rel="stylesheet" type="text/css" href="./css/PHPStarter.css">
|
||||||
<link rel="icon" type="image/x-icon" href="./icons/favicon.ico">
|
<link rel="icon" type="image/x-icon" href="./icons/favicon.ico">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>PHPStarter</h1>
|
<h1><?= $PHPStarter->message ?></h1>
|
||||||
<script type="text/javascript" src="./js/PHPStarter.js"></script>
|
<script type="text/javascript" src="./js/PHPStarter.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
16
src/DB.php
16
src/DB.php
|
|
@ -3,12 +3,24 @@
|
||||||
namespace PHPStarter;
|
namespace PHPStarter;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
use RuntimeException;
|
use PDOException;
|
||||||
|
|
||||||
class DB
|
class DB
|
||||||
{
|
{
|
||||||
public static function connect(): PDO
|
public static function connect(): PDO
|
||||||
{
|
{
|
||||||
throw new RuntimeException("DB connection not implemented yet.");
|
try
|
||||||
|
{
|
||||||
|
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_NAME']};port={$_ENV['DB_PORT']};unix_socket={$_ENV['DB_SOCKET']}";
|
||||||
|
$pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASS']);
|
||||||
|
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
||||||
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
}
|
||||||
|
catch (PDOException $exception)
|
||||||
|
{
|
||||||
|
die("Database connection failed: " . $exception->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pdo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue