jribeiro
jribeiro

Reputation: 3463

PHP Pdo no column names in array

I have the following query in PHP:

$stmt = $conn->prepare('SELECT 
        date, 
        avg(sells) as sells
FROM `table` WHERE product_id = :id group by date having count(id) > 1');
$stmt->execute(array('id' => $product));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

As expected this returns me:

array(2) { 
    [0]=> array(2) { 
        ["date"]=> "2013-1-11" ["sells"]=> "73.5000" 
    } 
    [1]=> array(2) { 
        ["date"]=> "2013-1-11" ["sells"]=> "77.0000" 
    }
}   

Anyway I can get the following output without looping with php?

array(2) { 
    [0]=> array(2) { 
        "2013-1-11",
        "73.5000" 
    } 
    [1]=> array(2) { 
        "2013-1-11",
        "77.0000" 
    }
}   

Upvotes: 3

Views: 3094

Answers (2)

Robert
Robert

Reputation: 8767

Since you stated that you are wanting to use JSON, try something along the following:

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);

This will give you a valid JSON structure based off of the data returned.

Upvotes: 1

deceze
deceze

Reputation: 522032

Instead of PDO::FETCH_ASSOC use PDO::FETCH_NUM.

Upvotes: 8

Related Questions