Here php function
(Update after reading comments etc.)
Yes, $db is defined outside and above function
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass//, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
function vatRates () {
$something = "test";
echo $something;
try {
$query_select_date = "SELECT CurrencyRate FROM 2013Currencies WHERE DateOfCurrencyRate = '2001-03-23'";
$sql_select_date = $db->prepare($query_select_date);
$sql_select_date->execute();
$data_select_date = $sql_select_date->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e){
echo "<br>DataBase Error: " .$e->getMessage();
}
catch (Exception $e) {
echo "General Error: ".$e->getMessage() .'<br>';
}
foreach ($data_select_date as $data) {
echo($data[CurrencyRate]);
}
}
And then call the function with vatRates();
In output get only word: test from $something. And php code that is after vatRates(); does not execute.
But if delete function vatRates () { and closing } then mysql query and foreach works.
Why mysql query and foreach does not work inside function (what need to correct)?
$dbis not defined inside the scope of the function: pass it as an argumentglobalshould not be the first recommendation to resolve function scope problems$dbparameter, isCurrencyRatedefined? You probably don't want to fill up your log with 'undefined CurrencyRate using "CurrencyRate" instead'