Php MySQL选择函数内部以访问其他地方的变量
基本上,我正在创建一个动态谷歌地图,从数据库中提取信息,连接正常,查询正常,但我无法从函数中访问变量($row) 我应该从php函数本身内部解析所有javascript吗?或者我可以让它在我的页面上保持静态,并通过直接调用javascript上方的函数(getCurrentMapData)访问变量吗Php MySQL选择函数内部以访问其他地方的变量,php,mysql,sql,function,Php,Mysql,Sql,Function,基本上,我正在创建一个动态谷歌地图,从数据库中提取信息,连接正常,查询正常,但我无法从函数中访问变量($row) 我应该从php函数本身内部解析所有javascript吗?或者我可以让它在我的页面上保持静态,并通过直接调用javascript上方的函数(getCurrentMapData)访问变量吗 function getCurrentMapData( $select ) { global $mysqli; $sql = $mysqli->query($select)
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>
我正在玩@
// ,
龙:,
zoomlevel:13
};
函数初始化()
{
psGlobals.latlng=新的google.maps.latlng(psGlobals.lat,psGlobals.lon);
buildMap();
psGlobals.dirRenderer=新的google.maps.DirectionsRenderer();
psGlobals.dirService=新的google.maps.DirectionsService();
}
函数buildMap()
{
变型肌肽,标记;
myOptions={
导航控制:对,
导航控制选项:{
样式:google.maps.NavigationControlStyle.ZOOM\u PAN
},
mapTypeControl:true,
scaleControl:false,
缩放:psGlobals.zoomlevel,
中心:psGlobals.latlng,
mapTypeId:google.maps.mapTypeId.HYBRID
};
psGlobals.map=new google.maps.map(document.getElementById(“ps_-map_1”),myOptions);
psGlobals.marker=新的google.maps.marker({
职位:psGlobals.latlng,
map:psGlobals.map,
标题:“
});
}
$(文档).ready(函数(){
初始化()
});
// ]]>
在php函数的末尾添加以下内容:
return $row;
这将使您的$row
输出失效,然后在代码中更改
getCurrentMapData( $select );
为此:
$row = getCurrentMapData($select);
您应该返回获取的数组
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)代码>$row=$sql->fetch_array()
这里$row
是函数getCurrentMapData($select)
的局部变量,不是吗?试着从函数返回$row
。啊,我就知道是这样的-谢谢sir@Jeff或者您可以简单地返回getCurrentMapData($select)代码>,因为$row
未在getCurrentMapData()中使用