PHPでDBに接続する為の機能 = PDO
PHPでデータベースに接続するための機能を「PDO」といいます。
PDOは「PHP Data Object」の頭文字をとったもの。
【PHPリファレンス】
DBに接続する
PDOを利用してデータベースの接続を行います。
sample.php
$db = new PDO('データベースの種類:host=接続先アドレス;dbname=データベース名', 'ユーザー名', 'パスワード');
(例)
(例).php
$db = new PDO('mysql:host=localhost;dbname=my_db', 'root', 'root');
SQLを実行する
DB接続の後、queryメソッドを利用してSQLを実行します。
sample.php
// testという名前のTableを作成する
$db->query('create table test(id INT)');
// SELECT文を実行する
$db->query('select * from samples where id = 1');