10
8

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.

PHP[PDO]のbindValue, bindParamでストアドプロシージャのoutを受け取ることはバグのためできない!

Last updated at Posted at 2013-12-21

概要

MySQLのストアドプロシージャを使って、ある1つの戻り値をPHPで受け取るために奮闘した結果をメモ代わりに残します。

結論

PHP[PDO]のbindValue, bindParamを使ってストアドプロシージャの引数を受け取ることは、仕様上できません。
彼のように頭を壁で叩きまくった結果コレにたどり着きましたが悶絶モノです。非常に気に入らない。
PHP.net - flannell ¶

コード

MySQLのストアドプロシージャ

use test;
delimiter //
	create procedure storedprocedure_test(in in_data varchar(255), out out_data varchar(255))
	begin
		select concat(in_data, ' is anpontan') into out_data;
	end;
//

PHP[PDO]からbind*でストアドプロシージャを叩く側

test.php
<?php
	$dbh = new PDO('mysql:host=localhost;', 'USER_NAME', 'PASSWORD');

	$in = 'wakisuke';
	$out = '***';

	/* 値の配列を渡してプリペアドステートメントを実行する */
	$sth = $dbh -> prepare('call test.storedprocedure_test(:in_data, :out_data);');
	$sth -> bindValue(':in_data', $in, PDO::PARAM_STR);
	$sth -> bindParam(':out_data', $out, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 255);
	$sth -> execute();
	$sth -> fetchAll();

	echo $out . "\n";
?>

結果

本当なら「wakisuke is anpontan」となることを期待しますが、残念ながら無理です。

> php ./test.php
> ***

PHPに投稿してくださったflannellさんのように、同じセッションでストアドプロシージャを叩いて、指定したユーザ変数をselectして得ましょう・・・。

10
8
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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?