0

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

7
  • 3
    Where do you get $db? Why don't you have error reporting on? Commented Aug 2, 2013 at 12:18
  • Are you missing a global $db at the start of the function, by any chance? When using functions, you have to specify what variables will require global scope. Commented Aug 2, 2013 at 12:18
  • 1
    $db is not defined inside the scope of the function: pass it as an argument Commented Aug 2, 2013 at 12:18
  • 1
    @LokiSinclair global should not be the first recommendation to resolve function scope problems Commented Aug 2, 2013 at 12:19
  • aside from the obvious missing $db parameter, is CurrencyRate defined? You probably don't want to fill up your log with 'undefined CurrencyRate using "CurrencyRate" instead' Commented Aug 2, 2013 at 12:24

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

2
function vatRates () {
   global $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]);
   }

}

1 Comment

Recommend passing $db as a parameter.
2

Function must be return something, a string, a array, a bolean, etc..

Better is (with arg $db passed):

function vatRates ($db) {
$CurrencyRate = "";

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

$CurrencyRate .= $data[CurrencyRate];
}
return $CurrencyRate;
}

To use it:

echo vatRates($db);

2 Comments

This is indeed 'the better way' to do it, it is no 'must do' though.
Regarding return here php.net/manual/en/function.return.php read that ends execution of current function. What if instead return $CurrencyRate; also need return $Unit; and other variables? return $CurrencyRate. $Units. $Etc;
1
function vatRates () {
   $something = "test";
   echo $something;
   global $db;

   /* Your sql code and foreach goes here*/

}

or

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

       /* Your sql code and foreach goes here*/

 }

1 Comment

Recommend passing $db as a parameter.
0

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']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.