PHP变量声明错误 - MySQL
问题描述:
我对php很新颖。我有一个简单的网站,通过一个MySQL数据库循环,并为每一行绘制多边形。它还为用户点击多边形时可以看到的每个多边形分配一个自定义属性(名称)。PHP变量声明错误 - MySQL
当用户点击多边形,他应该看到变量“BNAME”
代码工作完美的价值时,我给你BNAME的坐标:
var bname = ('.$row[coord1].');
的代码失败时,我给你BNAME另一列是一个字符串:
var bname = ('.$row[name].');
数据库中的所有数据是文本格式的,我怀疑,也许我使用了错误的语法从日返回一个字符串值电子数据库?我需要分配BNAME在数据库中的名称列..
这是全码:
$findmap= 'SELECT * FROM build2';
if(!$result = $con->query($findmap)){
die('There was an error running the query
[' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {
echo '
var polypath = [new google.maps.LatLng('.$row[coord1].'),
new google.maps.LatLng('.$row[coord2].'),
new google.maps.LatLng('.$row[coord3].'),
new google.maps.LatLng('.$row[coord4].'),
new google.maps.LatLng('.$row[coord5].'),
new google.maps.LatLng('.$row[coord6].'),
new google.maps.LatLng('.$row[coord1].')
];
var bname = ('.$row[name].');
var dxbmap = new google.maps.Polygon({
custom: bname,
path:polypath,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:1,
fillColor:"#0000FF",
fillOpacity:0.1
});
dxbmap.setMap(map);
google.maps.event.addListener(dxbmap, "click", function() {
alert(this.custom);
});
';
}
}
?>
答
您需要检查您的控制台在浏览器中,我认为JS错误是你是什么遇到。试试这个
$findmap= 'SELECT * FROM build2';
if(!$result = $con->query($findmap)){
die('There was an error running the query
[' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {
echo '
var polypath = [new google.maps.LatLng('.$row[coord1].'),
new google.maps.LatLng('.$row[coord2].'),
new google.maps.LatLng('.$row[coord3].'),
new google.maps.LatLng('.$row[coord4].'),
new google.maps.LatLng('.$row[coord5].'),
new google.maps.LatLng('.$row[coord6].'),
new google.maps.LatLng('.$row[coord1].')
];
var bname = \''.$row[name].'\';
var dxbmap = new google.maps.Polygon({
custom: bname,
path:polypath,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:1,
fillColor:"#0000FF",
fillOpacity:0.1
});
dxbmap.setMap(map);
google.maps.event.addListener(dxbmap, "click", function() {
alert(this.custom);
});
';
}
}
?>
根据您当前的代码的控制台日志,我认为会显示类似
Uncaught ReferenceError:
括号不封装的字符串,是你试图用这些做什么?
+0
感谢您的详细解释和完美的工作! :) – 1dxb
试试像这样: - 'var bname =(“$ row ['name']”);或者 var bname =($ row ['name']);'并检查。 –
如果它是一个字符串,我认为它需要在引号中。检查开发者控制台,抛出什么错误? – chris85
如果我理解你的问题,那么你需要在“var polypath”之前添加