0

ある PHP ドキュメント ページから例をコピーして貼り付けます。ケースが似ているためです。DBH->fetch() を使用して MySQL クエリを実行すると、配列が取得されます。

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>

出力は次のようになります。

Fetch all of the remaining rows in the result set:
Array
(
    [0] => Array
        (
            [name] => pear
            [0] => pear
            [colour] => green
            [1] => green
        )

    [1] => Array
        (
            [name] => watermelon
            [0] => watermelon
            [colour] => pink
            [1] => pink
        )
)

「名前付き」配列要素のみを返し、数値インデックスを持つものを削除するようにドライバーに指示する方法はありますか? 何かのようなもの:

Fetch all of the remaining rows in the result set:
Array
(
    [0] => Array
        (
            [name] => pear
            [colour] => green
        )

    [1] => Array
        (
            [name] => watermelon
            [colour] => pink
        )
)

前もって感謝します、シモーネ

4

1 に答える 1

0

Fetch_Assoc は名前付き配列のみを返します。これが変更後のコードです。

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetch_assoc();
print_r($result);
?>
于 2015-06-23T16:36:57.760 に答える