9
11

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 5 years have passed since last update.

Laravel5.1で素のPDOを使う

Last updated at Posted at 2015-12-01

裏でPDO使ってるのだから、わざわざ定義しなくても使えるだろうと思っておりましたが、オフィシャルページで記述を見つけたので試してみました。

めんどくさいのでroute.phpに書きます。

create table members
(
	id int,
	name varchar(32)
);

みたいなテーブルがることが前提。

Route::get('pdo',function(){

	$name = "hoge";

	//pdo
	//後はいつもお馴染み
	$dbh = DB::connection()->getPdo();

	$stmt = $dbh->prepare("select * from members where name = :name");
	$stmt->bindParam(':name',$name);
	$stmt->execute();

	foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row)
	{
		echo $row['id']." ".$row['name']."<br>";
	}

	return;
});

こんな感じ。

PDO::FETCH_ASSOC等でエラーがでる場合は、use PDO;とする。

$dbh = DB::connection()->getPdo();

とすれば後は普通のPDO。

9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?