MySQL选择内部函数来访问其他地方的变量

时间:2012-09-18 04:23:05

标签: php mysql sql function

基本上我正在创建一个动态谷歌地图,从数据库中提取信息,连接正常,查询很好 - 但我无法从我的函数访问我的变量($ row)。

我应该从php函数本身解析所有的javascript吗?或者我可以在我的页面上静态并通过直接在javascript上方调用函数(getCurrentMapData)来访问变量吗?

function getCurrentMapData( $select ) 
{
    global $mysqli;

    $sql = $mysqli->query($select);

    if(!$sql) {
       printf("%s\n", $mysqli->error);
       exit();
    }   

    $row = $sql->fetch_array(); 

}

这是我们的开头:

<?php

    $select = "SELECT `id`, `title`, `address`, `lat`, `lng`, `date` FROM `globalmap` ORDER BY `id` desc "; 

    getCurrentMapData( $select );

?>

                <h3>
                    I'm Currently Playing @ 
                <?php echo $row['title'] ?>
                </h3>
                <div id="map">
                    <div id="ps_map_1" class="mapCon">
                    </div>
                </div>
                <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script>
                    // <![CDATA[


                        var psGlobals = {
                            address: "<?php echo $row['address'] ?>",
                            lat: <?php echo $row['lat'] ?>,
                            lon: <?php echo $row['lng'] ?>,
                            zoomlevel: 13
                        };

                        function initialize() 
                        {
                            psGlobals.latlng = new google.maps.LatLng(psGlobals.lat, psGlobals.lon);
                            buildMap();
                            psGlobals.dirRenderer = new google.maps.DirectionsRenderer();
                            psGlobals.dirService = new google.maps.DirectionsService();
                        }

                        function buildMap()
                        {
                            var myOptions, marker;
                            myOptions = {
                                  navigationControl: true,
                                  navigationControlOptions: {
                                      style: google.maps.NavigationControlStyle.ZOOM_PAN
                                    },

                                  mapTypeControl: true,
                                  scaleControl: false,
                                  zoom: psGlobals.zoomlevel,
                                  center: psGlobals.latlng,
                                  mapTypeId: google.maps.MapTypeId.HYBRID
                            };
                            psGlobals.map = new google.maps.Map(document.getElementById("ps_map_1"), myOptions);
                            psGlobals.marker = new google.maps.Marker({
                                position: psGlobals.latlng, 
                                map: psGlobals.map,
                                title: "<?php echo $row['title'] ?>"
                            });

                        }

                        $(document).ready(function(){
                            initialize()
                        });

                    // ]]>

                    </script>           

2 个答案:

答案 0 :(得分:1)

在你的php函数中最后添加:

return $row;

这将输出您的$row功能,然后在您的代码中更改

getCurrentMapData( $select );

到此:

$row = getCurrentMapData($select);

答案 1 :(得分:1)

你应该返回获取的数组

function getCurrentMapData( $select )  
{ 
    global $mysqli; 

    $sql = $mysqli->query($select); 

    if(!$sql) { 
       printf("%s\n", $mysqli->error); 
       exit(); 
    }    

    return $sql->fetch_array();  

} 

然后您应该将此函数的结果分配给$row变量

$row = getCurrentMapData($select);