0

私は現在、コマンドライン、PHP MyAdminを介してmysqlに直接配置するか、MysqlWorkbenchなどのMysqlエディターで実行すると機能するmysqlUnionクエリを持っています。PHPクラスを介してクエリを実行すると、2つの重複する行が返されます。

クエリ:

SELECT `n`.`draftrd`, `n`.`draftteam`, `n`.`nflteam`, `nt`.`logo`, 
    `nt`.`name`, `nt`.`nflcity`,
    `p`.`class`, `p`.`first_name`, `p`.`last_name`
FROM `players` as `p` 
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`nflteam` = `nt`.`nflid` 
WHERE `n`.`playerid` = 2007000001
UNION
SELECT `n`.`draftrd` as `draftRd`, `n`.`draftteam` as `draftTm`, `n`.`nflteam`, 
    `nt`.`logo`, 
    `nt`.`name` as `draftName`, `nt`.`nflcity` as `draftCity`, `p`.`class`, 
    `p`.`first_name`, `p`.`last_name`
FROM `players` as `p` 
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`draftteam` = `nt`.`nflid` 
WHERE `n`.`playerid` = 2007000001

エディターでは、次のように返されます。

+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| draftrd | draftteam | nflteam | logo        | name    | nflcity  | class | first_name | last_name | tableid |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| 2       | 2         | 12      | giants.png  | Giants  | New York | 2007  | Martellus  | Bennett   |       1 |
| 2       | 2         | 12      | cowboys.png | Cowboys | Dallas   | 2007  | Martellus  | Bennett   |       1 |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
2 rows in set (0.00 sec)

PHPの戻り値は次のとおりです:(var_dumpバージョン)

array(18) { 
    [0]=> string(1) "2" 
    ["draftrd"]=> string(1) "2" 
    [1]=> string(1) "2" 
    ["draftteam"]=> string(1) "2"
    [2]=> string(2) "12" 
    ["nflteam"]=> string(2) "12" 
    [3]=> string(10) "giants.png" 
    ["logo"]=> string(10) "giants.png" 
    [4]=> string(6) "Giants" 
    ["name"]=> string(6) "Giants" 
    [5]=> string(8) "New York" 
    ["nflcity"]=> string(8) "New York" 
    [6]=> string(4) "2007" 
    ["class"]=> string(4) "2007"
    [7]=> string(9) "Martellus"
    ["first_name"]=> string(9) "Martellus"
    [8]=> string(7) "Bennett" 
    ["last_name"]=> string(7) "Bennett" 
}

問題が何であるか、なぜ正しく機能しないのかわかりません。一時テーブルを作成してクエリを保存しようとしましたが、それでも重複した行が表示されます。これを行う簡単な方法や、なぜそれが起こっているのかについての説明はありますか?PHPでクエリをダンプして、問題がその構造にあるかどうかを確認しましたが、ダンプされたクエリは、コマンドラインで実行したときに探している行を返します。

4

1 に答える 1

1

PHPのMySQLハンドルを使用すると、さまざまな方法で結果の配列を反復処理できます。たとえば、キー(例:)で連想配列を反復処理するecho $result['draftteam'];か、通常の配列(例:)でインデックスを反復処理するかを選択できますecho $result[2];

このコマンドを使用するfetch_array()と、デフォルトでは、両方のタイプが同時にフェッチされます。これにより、提供したアラリーが生成されます。

クエリに基づいて動的テーブルを作成しようとしている場合(つまり、各列名を下のs<th>に空にする前に各列名を書き込むテーブル)は、コマンドではなくコマンドを使用する必要があります。これにより、配列内のテキストキーのみが返されます。<td>fetch_assoc()fetch_array()

while($arr = mysqli_fetch_assoc($sql)) {
    foreach($arr as $key => $value){
        echo $key .' => '. $value .'<br />'."\n";
    }
}

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://php.net/manual/en/mysqli-result.fetch-array.php

于 2012-12-03T21:01:01.943 に答える