0

Here is the SQL for one of my queries:

private static function getFixedFare($post_code_a, $post_code_b) {

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

    try {

        $query =   "SELECT VehicleSystemId, Fare FROM tblfixedfares
                    WHERE ShortPostCodeA = '$post_code_a'
                    AND ShortPostCodeB = '$post_code_b'
                    AND DayHalf = :day_half
                    AND VehicleSystemId IN ('Car', '6B')";

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

        $stmt->bindParam(':day_half', self::$day_half, PDO::PARAM_STR);

        $stmt->execute();

        $result = $stmt->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);

        $stmt->closeCursor();

        $dbh = null;

        var_dump($result['Car']);

    }

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

}

When I do a var_dump($result['Car']); I get the following response:

array(1) {
  [0]=>
  array(1) {
    ["Fare"]=>
    string(3) "100"
  }
}

How would I go about getting the value '100'... So basically the $result for ['Car'] ['Fare'] ?

Edit ::

var_dump($cars); shows:

    array(2) {
       ["Car"]=>
       array(1) {
       [0]=>
       array(1) {
       ["Fare"]=>
       string(3) "100"
       }
     }
     ["6B"]=>
     array(1) {
     [0]=>
     array(1) {
     ["Fare"]=>
     string(3) "700"
     }
    }

I've tried; $result['Car']['Fare'] - but this is returning NULL;

2 Answers 2

1

Try this:

$value = $result['Car'][0]['Fare'];

The value for $result['Car'] is an array, with the index of the first element being 0; the value of that first element is an array again (one item, key 'Fare', value 100).

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

Comments

1

You'd get the value '100' exactly as you explained it, $fare = $result['Car']['Fare']; //$fare is 100

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.