2

かなり大きな問題に気づきました。2 つのテーブルを結合すると、両方に ID という列があるため、後で PHP の等式で間違ったテーブル ID が使用されます。

簡単な解決策は列名を変更することですが、すべてのテーブルで name と呼ばれる列やそれらの多くで title と呼ばれる列を含め、データベース全体に他の標準もあります。

これを回避する方法はありますか、またはデータベース全体の名前を変更して、重複する列がないようにする必要があります。

参照用コード

$criteria = "SELECT *
  FROM voting_intention,electors
  WHERE  voting_intention.elector = electors.ID
  AND electors.postal_vote = 1
  AND voting_intention.date = (select MAX(date)
    from voting_intention vote2      
    where voting_intention.elector = vote2.elector)
  AND electors.telephone > 0"


function get_elector_phone($criteria){
    $the_elector = mysql_query("SELECT * $criteria"); while($row = mysql_fetch_array($the_elector)) {
    return $row['ID']; }     
4

3 に答える 3

0

「Gunnx」ソリューションは完全に受け入れられますが、結果の ID 列のみを使用しているように見えるため、代替案を提示したいと思います。

SELECT
    electors.ID
FROM
    voting_intention,electors
....
于 2012-06-29T08:25:03.240 に答える
0

mysql_fetch_rowデータを数値配列として取得します

于 2012-06-29T08:15:24.840 に答える
0

これを行うのを支援するために、この関数を作成しました。基本的に、関連する配列のフィールド名にテーブル名を追加します。

while($row = mysql_fetch_array($the_elector))
{
    return $row['ID'];
}

になるだろう

while($row = mysql_fetch_table_assoc($the_elector))
{
    return $row['voting_intention.ID'];
}

関数:

function mysql_fetch_table_assoc($resource)
{
    // function to get all data from a query, without over-writing the same field
    // by using the table name and the field name as the index

    // get data first
    $data=mysql_fetch_row($resource);
    if(!$data) return $data; // end of data

    // get field info
    $fields=array();
    $index=0;
    $num_fields=mysql_num_fields($resource);
    while($index<$num_fields)
    {
        $meta=mysql_fetch_field($resource, $index);
        if(!$meta)
        {
            // if no field info then just use index number by default
            $fields[$index]=$index;
        }
        else
        {
            $fields[$index]='';
            // deal with field aliases - ie no table name ( SELECT T_1.a AS temp, 3 AS bob )
            if(!empty($meta->table)) $fields[$index]=$meta->table.'.';
            // deal with raw data - ie no field name ( SELECT 1, MAX(index) )
            if(!empty($meta->name))  $fields[$index].=$meta->name; else $fields[$index].=$index;
        }
        $index++;
    }
    $assoc_data=array_combine($fields, $data);
    return $assoc_data;
}

?>
于 2012-06-29T08:19:07.193 に答える