Make sure to specify the full namespace for the "string $class_name" parameter and not a partial one, as it won't find it. For example:
<?php
namespace Root(backslash)FirstLevel
{
public static function Test($result)
{
return mysqli_fetch_object($result, 'SecondLevel\\MyClass');
}
}
?>
... will not work but this will:
<?php
namespace Root(backslash)FirstLevel
{
public static function Test($result)
{
return mysqli_fetch_object($result, 'Root\\FirstLevel\\SecondLevel\\MyClass');
}
}
?>
mysqli_result::fetch_object
mysqli_fetch_object
(PHP 5)
mysqli_result::fetch_object -- mysqli_fetch_object — 結果セットの現在の行をオブジェクトとして返す
説明
オブジェクト指向型
object mysqli_result::fetch_object
([ string
$class_name
[, array $params
]] )手続き型
mysqli_fetch_object() は、結果セットの現在の行を オブジェクトで返します。このオブジェクトは、結果セットのフィールド名を 属性として持ちます。
mysqli_fetch_object() がオブジェクトのプロパティを設定するのは、 オブジェクトのコンストラクタをコールする前であることに注意しましょう。
パラメータ
-
result -
手続き型のみ: mysqli_query()、mysqli_store_result() あるいは mysqli_use_result() が返す結果セット ID。
-
class_name -
インスタンス化してプロパティを設定後に返すクラスの名前。 省略した場合は stdClass オブジェクトを返します。
-
params -
オプションのパラメータの配列で、
class_nameオブジェクトのコンストラクタに渡します。
返り値
取得した行に対応する文字列プロパティを有するオブジェクトを返します。
もし結果セットにもう行がない場合には NULL を返します。
注意: この関数により返されるフィー ルド名は 大文字小文字を区別 します。
注意: この関数は、 NULL フィールドに PHPの
NULL値を設定します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.0.0 | さまざまな型のオブジェクトで結果を返せるようになりました。 |
例
例1 オブジェクト指向型
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* オブジェクトの配列を取得します */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
/* 結果セットを開放します */
$result->close();
}
/* 接続を閉じます */
$mysqli->close();
?>
例2 手続き型
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* オブジェクトの配列を取得します */
while ($obj = mysqli_fetch_object($result)) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
/* 結果セットを開放します */
mysqli_free_result($result);
}
/* 接続を閉じます */
mysqli_close($link);
?>
上の例の出力は以下となります。
Pueblo (USA) Arvada (USA) Cape Coral (USA) Green Bay (USA) Santa Clara (USA)
参考
- mysqli_fetch_array() - 結果の行を連想配列・数値添字配列あるいはその両方の形式で取得する
- mysqli_fetch_assoc() - 結果の行を連想配列で取得する
- mysqli_fetch_row() - 結果の行を数値添字配列で取得する
- mysqli_query() - データベース上でクエリを実行する
- mysqli_data_seek() - 結果の任意の行にポインタを移動する
Alex
27-Jan-2012 04:36
benpptung at tacol dot biz
08-Aug-2009 08:04
I don't know why no one talk about this.
fetch_object is very powerful since you can instantiate an Object which has the methods you wanna have.
You can try like this..
<?php
class PowerfulVO extends AbstractWhatEver {
public $field1;
private $field2; // note : private is ok
public function method(){
// method in this class
}
}
$sql = "SELECT * FROM table ..."
$mysqli = new mysqli(........);
$result = $mysqli->query($sql);
$vo = $result->fetch_object('PowerfulVO');
?>
Note : if the field is not defined in the class, fetch_object will add this field for you as public.
The method is very powerful, especially if you want to use a VO design pattern or class mapping feature with Flex Remoting Object( Of course, you need to have ZendAMF or AMFPHP ..framework)
Hope this help and open new possibilities for you
peterbelm at g[oogle]mail dot com
18-Aug-2008 07:10
If your SQL code selects columns with empty names like so:
SELECT id as ``...
You will get a fatal error "Cannot access empty property", this took me a while to track down!
Obviously your SQL really shouldn't do that, and should be fixed but I'm going to submit a feature request to ask for a better error message for that.
