0

I have a class called TariffFareController, and I am getting the correct fare from the database as follows:-

class TariffFareController {

public static function getTourFare($fieldTour) {

       $pdo = new SQL();
       $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

       try {

           $query =   "SELECT Fare FROM tbltours
                       WHERE TourName = '$fieldTour'";

           $stmt = $dbh->prepare($query);

           $stmt->execute();

           $result = $stmt->fetchColumn();

           $stmt->closeCursor();

           return $result;

           $dbh = null;

       }

       catch (PDOException $pe) {
           die("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString);
       }

}

}

I then want to be able to check the result in my private get fare function:-

private static function getFare($int_terminate) {

    // $tour_fare = (the result from getTourFare)

        if ($tour_fare != null) {

            // do something

        }

        else {

        // do something else

        }

    }

How would I go about getting the $result from getTourFare and using this in the private getFare function?

Any help would be much appreciated!

2
  • Statements after return $result; don't get executed. You need to move $dbh = null; up or it won't be done. Commented Mar 31, 2013 at 8:36
  • I've removed that, thanks Commented Mar 31, 2013 at 8:56

2 Answers 2

3

If both functions are in the same class, simply do

$tour_fare = self::getTourFare();

else you can call it from outside the class using

$tour_fare = TariffFareController::getTourFare();

Please notice that in the getTourFare() function the line $dbh = null; will never get executed 'cause you're returning from the function before executing it. I have also to say that I think that that line is quite useless cause the $dbh variable will go out of scope anyway.

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

2 Comments

self::getTourFare(); comes up with errors as $fieldTour is a required parameter. I've changed this to self::getTourFare($fieldTour); then it shows an error as $fieldTour is undefined, any suggestions?
Well, I did not place parameters 'cause I supposed you knew how to handle them... If you have to call getTourFare from getFare, and getTourFare required a parameter $fieldTour then also getFare will need that parameter so to be able to pass it to the other function.
0

You can do some like this.

Call to getTourFare through getFare and if return data, do can process data instead return false you do any thing.

class TariffFareController {


private static function getFare() {
    if (self::getTourFare("any_field")) {
        /* Data success */
    } else {
        /* No data */
    }
}
public static function getTourFare($fieldTour) {

   $pdo = new SQL();
   $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

   try {

       $query =   "SELECT Fare FROM tbltours
                   WHERE TourName = '$fieldTour'";

       $stmt = $dbh->prepare($query);

       if (!$stmt->execute()) return false;

       if ($stmt->rowCount() > 0) {
           $result = $stmt->fetchColumn();

           $stmt->closeCursor();

           return $result;
       } else {
           return false;
       }

       $dbh = null;

   }

   catch (PDOException $pe) {
       die("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString);
   }

}

}

2 Comments

I didn't think you could use $this-> within static content
Sure, I'm sorry.. don't see the static.

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.