0
0

More than 1 year has passed since last update.

PHPでDBに接続してSQLを実行する

Posted at

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');
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