電子書籍の厳選無料作品が豊富!

今まで、以下のような記述でレコード数を取得していました。
$cnt = $dbh->selectrow_array("SELECT count(*) from table_hoge");

しかし、以下のようにしないとエラーになるサーバがあります。
($cnt) = $dbh->selectrow_array("SELECT count(*) from table_hoge");

違いとしては、上はMySQL5.0系、下はMySQL5.1系です。
バージョンによる違いかどうか、はっきりしていないのですが、
このような仕様の変更があったのでしょうか?
もしくは、上の書き方はそもそも間違いだったのでしょうか?

A 回答 (1件)

以下のような記述が見つかりました。


If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that. Also, in a scalar context, an "undef" is returned if there are no more rows or if an error occurred. That "undef" can't be distinguished from an "undef" returned because the first field value was NULL. For these reasons you should exercise some caution if you use "selectrow_array" in a scalar context, or just don't do that.

要約すると、配列用だからスカラに入れるのは要注意!or 入れちゃダメ。
スカラに入れるならselectrow_arrayrefで、

 $ary_ref = $dbh->selectrow_arrayref($statement);

ってしてね。
という事らしいです。

参考URL:http://blue21.ddo.jp/man/man2html.html?lang=en&c …
    • good
    • 0
この回答へのお礼

ありがとうございます。
やはり今までの書き方が間違っていたんですね。
かなり色々な所で使ってしまっています。
大変助かります。ありがとうございました。
参考URLも何やらいろいろと載っていて、いい感じです。

お礼日時:2009/11/06 15:44

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!