0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PDO別ファイルから読み込み

Posted at

PDOいまだによくわかってないけど
ひとまずこれで動いたよメモ

別ファイルから読み込み


------1-1.PHP
require_once("pdo.php");
$pdo=db("DBNAME"); //db名を引数として渡す
$sql ="select * from user";
$stmt= $pdo->query($sql); //変数なし使用
$i =0;
foreach($stmt as $key => $val){
   $users[$val['id']] = $val['name']; //NAME[ID]にしたかった
   $i++;
}

--------pdo.php(db接続用)
function db($dbname){ //db名を引数として受け取る
        $dsn = 'HOST'.$dbname;'charset=utf8';
        $user = ''; //USER
        $pass = ''; //PASS
        //db接続
        try{
            $dbh = new PDO($dsn,$user,$pass,[
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            ]);
            return $dbh;
        } catch(PDOException $e){
            echo '接続失敗'. $e->getMessage();
            exit();
        };
   }

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?