John Arzaga
John Arzaga

Reputation: 103

Get a single data using PDO and store in a variable

I have this php script to update data. I need to store result from a query using SUM. I have the code below.

$query=$DBcon->prepare("SELECT SUM(fee) FROM users WHERE list_no=:list_no");
$query->bindParam(':list_no', $sp_no);
$query->execute();
$result = $query -> fetch();
echo $result["figure"];

It displays ARRAY...

Upvotes: 0

Views: 63

Answers (3)

Toney Dias
Toney Dias

Reputation: 51

I think changing echo to print_r($result); will give you the answer. You can also try var_dump on each variable to debug the issues

Upvotes: 0

mscho
mscho

Reputation: 974

The shortest solution is:

echo $query->fetchColumn();

Upvotes: 0

fire
fire

Reputation: 21531

If your selecting a single column you can do this:

$result = $query->fetch(PDO::FETCH_COLUMN);
echo $result;

Upvotes: 1

Related Questions