我无法在我的 php / mysql Select 查询中获取全局变量。
这是我目前拥有的代码,它不会从我的数据库中返回任何值。
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
window.onload = function()
{
document.getElementById('wall').innerHTML="<?php zoomLevelOne();?>";
}
</SCRIPT>
<?php
$startBrick = rand (1, 4);
function zoomLevelOne()
{
global $startBrick;
$brickNo = rand (1, 4);
$con = mysql_connect("localhost","root","password");
if(!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("wall", $con);
$result = mysql_query("SELECT * FROM bricks WHERE BrickNo=$startBrick");
$brick = mysql_fetch_array($result);
mysql_close($con);
echo $brick['year'];
}
?>
</HEAD>
<BODY>
<DIV id='wall'>
</DIV>
</BODY>
</HTML>
如果我将选择查询的结尾更改为:
WHERE BrickNo=$brickNo");
所以整个代码是这样的:
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
window.onload = function()
{
document.getElementById('wall').innerHTML="<?php zoomLevelOne();?>";
}
</SCRIPT>
<?php
$startBrick = rand (1, 4);
function zoomLevelOne()
{
global $startBrick;
$brickNo = rand (1, 4);
$con = mysql_connect("localhost","root","password");
if(!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("wall", $con);
$result = mysql_query("SELECT * FROM bricks WHERE BrickNo=$brickNo");
$brick = mysql_fetch_array($result);
mysql_close($con);
echo $brick['year'];
}
?>
</HEAD>
<BODY>
<DIV id='wall'>
</DIV>
</BODY>
</HTML>
这对我来说似乎是正确的,因为我所做的只是将局部变量更改为全局变量,但它不起作用,所以也许我做错了什么,或者只是我的编码不佳。
谁能帮我。