3

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" 
    }
}   
5
  • 1
    What is your purpose for not knowing the columns? Commented Jan 14, 2013 at 4:06
  • 1
    I want to output the result as JSON directly. For that I need to drop the columns names Commented Jan 14, 2013 at 4:07
  • Why do you need to drop the column names for output to JSON? It makes it easier to work with to have column names even in JSON. Commented Jan 14, 2013 at 4:08
  • @jribeiro That doesn't make much sense. What's wrong with JSON objects with named properties? Commented Jan 14, 2013 at 4:08
  • Yup I know... Plugin implementation requirements. Commented Jan 14, 2013 at 4:11

2 Answers 2

8

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

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

Comments

1

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.

2 Comments

Yeah but that will output the column names as well.
Correct, I'm not sure why you would not want to reference the string:value structure for your data.

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.