使用環境(ローカル環境)
・MAMP
・phpMyAdmin
データベース定義(使用するカラム)
・id
・name
・email
・password
手順
データベースとの接続
env.php
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'user');
define('DB_USER', 'root');
define('DB_PASS', 'root');
?>
dbconnect.php
<?php
require_once('env.php');
function connect()
{
$host = DB_HOST;
$db = DB_NAME;
$user = DB_USER;
$pass = DB_PASS;
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
try{
$pdo = new PDO($dsn, $user, $pass, $options);
}catch(PDOException $e){
echo 'error'. $e->getMessage();
}
}
?>