1

I am having trouble getting a global variable work within my php / mysql Select query.

Here is the code I have at the moment, it doesn't return any value from my database.

<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>

If I change the end of my select query to:

WHERE BrickNo=$brickNo");

so the whole code is this:

<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>

It seems right to me as all I am doing is changing a local variable to a global one but it isn't working so maybe I am doing something wrong or its just my poor coding.

Can anyone help me.

user1041528
  • 25
  • 2
  • 6
  • It was taken from a tutorial I watched on YouTube. Why is it bad – user1041528 Jan 20 '13 at 15:41
  • by not working I mean, it isn't outputting the information from my database on the screen. the one with the local variable does output the information. – user1041528 Jan 20 '13 at 16:19
  • 1
    Globals are [**bad practice**](http://stackoverflow.com/questions/5166087/php-global-in-functions).If your function requires arguments to run, make them explicit and pass them to function ie `zoomLevelOne($startBrick)` – david strachan Jan 20 '13 at 17:03

1 Answers1

0

The answer to me looks fairly simple. The code is calling the function before it declares the variable. Just move the script block to after you define the function, just before the tag.

<HTML>
<HEAD>
<?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'];
}
?>
<SCRIPT type="text/javascript">
window.onload = function()
{
    document.getElementById('wall').innerHTML="<?php zoomLevelOne();?>";
}
</SCRIPT>
</HEAD>
<BODY>
<DIV id='wall'>
</DIV>
</BODY>
</HTML>
david.tanner
  • 529
  • 3
  • 8
  • 13