My programming level is fairly elementary so a 'coding for idiots' type of explanation would be great...
I have the following code:
<?php
$host = 'localhost';
$user = 'user';
$password = 'password';
$db_name = 'db_name';
$connect = mysqli_connect($host, $user, $password);
mysqli_select_db($connect, $db_name) or die ("Couldn't connect");
function roll_die() {
$throw = rand(1, 6);
return $throw;
}
function get_subtotal() {
$query = "SELECT * FROM throws";
$result = mysqli_query($connect, $query);
while ($row = $result->fetch_assoc()) {
echo $row['value']."<br>";
}
}
?>
I get an error because the '$connect' in the function subtotal() is apparently undefined. How can that be if it's defined at the top of the page? Wouldn't that make it a global function?
Please don't just give me the correct code to fix this. Could you explain what's going on and how PHP defines and stores variables?
Thanks!
Ok, I found the answer.
At the top of the page:
$connect = mysql_connect($host, $user, $password);
Then within the function:
global $connect;
Actually, I found the answer in another question which was essentially asking the same thing, so apologies for the repost.