0

Input :

$sql1 = "SELECT COUNT(*) FROM matchTrip where userTripId = :tripId";

$stmt1 = $this->db->prepare($sql1);

$stmt1->bindParam(':tripId', $trip, PDO::PARAM_INT);                                              

$temp = $stmt1->fetchObject();

echo(json_encode($temp));

Output:

 How to take value from array : 

 of which json_encode looks like this: {"COUNT(*)":"7"}

Any help will be appreciated.

5
  • echo $array['COUNT(*)']; Commented Sep 2, 2013 at 9:17
  • Why not iterate over the array and perform the check? Commented Sep 2, 2013 at 9:17
  • Why not print $temp instead of encode it..?? Commented Sep 2, 2013 at 9:18
  • @Gautam3164 : when I print only $temp then it says print only this : Array Commented Sep 2, 2013 at 9:20
  • @Chiel92 : Actually I dont know how to iterate? Commented Sep 2, 2013 at 9:27

5 Answers 5

1

Why don't you just give the column an alias in the SQL itself?

$sql1 = "SELECT COUNT(*) as myCount FROM matchTrip where userTripId = :tripId";

Makes the rest easier to work with.

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

2 Comments

Ok.But how to print myCount?
Got it.$temp->myCount.Thanks
0

Do you mean json_decode? You can just put it between quotes and it should work; $array["COUNT(*)"].

But you can also add "AS myCount" to your SQL.

Comments

0

if to get rid of all the useless stuff from your code

$sql  = "SELECT COUNT(*) FROM matchTrip where userTripId = ?";
$stmt = $this->db->prepare($sql);
$stmt->execute(array($table_of_user[$i]));
$count = $stmt->fetchColumn();

echo $count;

Comments

0

So why not just fecth as array?

$temp = $stmt1->fetch(PDO::FETCH_ASSOC);
echo $temp['COUNT(*)'];

Comments

0

Just use like this:

$json = json_encode($temp);

echo $json->{'COUNT(*)'}; // 7

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.