-2

I am trying to get the (float) value from a database, but when I print the result it is showing as 'Array' instead of the value (20).

Here is the code:-

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->fetchAll();

           $stmt->closeCursor();

           print_r($result[0]);

           return $result;

           $dbh = null;

       }

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

}

I know it is only selecting one value and shouldn't return an array of values. I believe the issue is $stmt->fetchAll();, but I'm not quite sure what this needs to be changed to?

4

1 回答 1

1

From fetchAll() documentation

PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name.

You can use fetchColumn() to fetch just a string result.

于 2013-03-30T11:44:26.187 回答