1

这是用建议更新的整个代码。还添加了新的列名。让我知道它是否正确。

<?php
    //Initialize your first couple variables
    $encodedString = ""; //This is the string that will hold all your location data
    $x = 0; //This is a trigger to keep the string tidy

    //Now we query to the database      
    $result = mysql_query("SELECT visitors_pb_list.first, visitors_pb_list.last, visitors_pb_list.ip, visitors_pb_list.landing_page as visitors_pb_list_landing_page, pb_list.address, pb_list.landing_page as pb_list_landing_page, ziplatlang.longitude, ziplatlang.latitude FROM visitors_pb_list LEFT JOIN pb_list ON visitors_pb_list.landing_page = pb_list.landing_page LEFT JOIN ziplatlang ON pb_list.zip = ziplatlang.zip_code WHERE landing_page='" . $pb_list_id . "' order by id desc");

    //Multiple rows are returned
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        //This is to keep an empty first or last line from forming, when the string is split
        if ( $x == 0 )
        {
             $separator = "";
        }
        else
        {
             //Each row in the database is separated in the string by four *'s
             $separator = "****";
        }
        //Saving to the String, each variable is separated by three &'s
        $encodedString = $encodedString.$separator.
        "<p class='content'><b>Lat:</b> ".$row['latitude'].
        "<br><b>Long:</b> ".$row['longitude'].
        "<br><b>Name: </b>".$row['first'].$row['last'].
        "<br><b>Address: </b>".$row['address'].
        "<br><b>IP: </b>".$row['ip'].
        "</p>&&&".$row['latitude']."&&&".$row['longitude'];
        $x = $x + 1;
    }        
?>
<input type="hidden" id="encodedString" name="encodedString" value="<?php echo $encodedString; ?>" />

如果你想知道我在哪里得到基本代码,它在这里:

http://www.macrostash.com/2011/09/17/demo-use-a-php-mysql-database-to-load-markers-on-a-google-map/#codesyntax_4

但是我正在尝试使其适应多个表。不能只用一个。需要使用visitors_pb_listfor循环。需要ziplatlang用于纬度和经度。需要使用通过列pb_list来桥接另外两个表。zip到目前为止还没有找到成功。

4

1 回答 1

0

您必须重命名所有具有相同名称的列,例如,如果所有三个表都有一个名称列:

SELECT 
visitors_pb_list.name as visitors_pb_list_name, 
pb_list.name as pb_list_name, 
ziplatlang.name as ziplatlong_name
FROM visitors_pb_list 
LEFT JOIN pb_list ON visitors_pb_list.landing_page = pb_list.landing_page 
LEFT JOIN ziplatlang ON pb_list.zip = ziplatlang.zip_code 
WHERE landing_page='" . $pb_list_id . "' order by id desc

在 php 中,我建议您按名称访问结果列,这是 PDO 的默认值

或使用 mysql 函数:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    //This is to keep an empty first or last line from forming, when the string is split
    if ( $x == 0 )
    {
         $separator = "";
    }
    else
    {
         //Each row in the database is separated in the string by four *'s
         $separator = "****";
    }
    //Saving to the String, each variable is separated by three &'s
    $encodedString = $encodedString.$separator.
    "<p class='content'><b>Lat:</b> ".$row['latitude'].
    "<br><b>Long:</b> ".$row['longitude'].
    "<br><b>Name: </b>".$row['name'];

    $x = $x + 1;
}
于 2013-11-13T22:52:56.517 回答