Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a MySQL table that contains the following columns:

id : title : message : date
1  : blah  : blah    : 27/12/2012

And I have queried it and stored it in an array. How do I get the individual columns if a certain ID. For example, I want to get the row where ID = 1 and get the title, message etc. And then I want row 2 and row 3. How do I get the information out of an array. Like

$array[1]["date"]; ( I tried it and it never worked)
share|improve this question
2  
How are you storing it in an array? Can you add the code where you convert the result into a php array? – MrGlass Dec 27 '12 at 15:42
And I have queried it and stored it in an array most likely you are storing it with 0-indexed array – dev-null-dweller Dec 27 '12 at 15:42
depends on how you have it stored in the array. – Pitchinnate Dec 27 '12 at 15:42
can you print your array so we can see what it looks like? – Steph Rose Dec 27 '12 at 15:42
what have you tried? – Drewdin Dec 27 '12 at 15:43

2 Answers

You basicly need to fetch the result as a associative array and loop through all the results. All your results will be in the form of

array(
'id' => 1, 'title' => 'blah',
'message' => 'blah',
'date' => '27/12/2012'
);

example for mysqli implementation of this: Mysqli Fetch Assoc Documentation

share|improve this answer
$array = array();
while ($row = fetch_assoc($result)) { // use your favorite fetch method here
    $array[$row['id']] = $row;
}

echo $array[1]["date"]; // now will work (if you have such id)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.