Question: 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)?

Answer: 
```
function vatRates ($db) {
   $something = "test";
   echo $something;

   $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);

   foreach ($data_select_date as $data) {
       echo($data[CurrencyRate]);
   }

}

```

call with vatRates($db)

Reason: $db is currently outside the visibility of your function, this passes it to the function.
Could be an issue with you not accessing the values in the array correctly. Try changing the foreach to this

```
foreach ($data_select_date as $data) { 
  echo($data['CurrencyRate']);
}

```