2
2

More than 3 years have passed since last update.

PHP ログイン認証の作成手順① 〜データベースと接続する〜

Posted at

使用環境(ローカル環境)

・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();
    }
}

?>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2