Page 1 of 1

SQL Query and Arrays [Solved]

Posted: Fri Sep 24, 2010 11:39 am
by christh
Hello - just a quick one

Is it possible to identify the entry for say... row 3, column 4 from a standard SQL query?

ie something like

$result = mysql_query(" SELECT first, second, third, fourth FROM Test")
echo $result[3,4] ...or....$result[third,4]

Thanks in advance.

Chris

Re: SQL Query and Arrays

Posted: Fri Sep 24, 2010 11:43 am
by John Cartwright
You need to iterate your data set first. Your best option if you want to lookup keys in array is to build your results into a array, then you can do whatever.

I.e.,

Code: Select all

$result = mysql_query(" SELECT first, second, third, fourth FROM Test");

$rows = array();
while ($row = mysql_fetch_assoc($result)) {
   $rows[] = $row;
}

// Third row, column name "fourth"
// But remember, arrays begin at a zero based key, so third row is identified as the [2] key
echo $result[2]['fourth'];
However, you generally want to query the exact data you want instead of pulling your entire data set and thus performing the filter.

Re: SQL Query and Arrays

Posted: Sun Sep 26, 2010 6:02 am
by christh
Excellent - thank you John

Re: SQL Query and Arrays [Solved]

Posted: Sun Sep 26, 2010 1:15 pm
by John Cartwright
Just so there is no confusion, I had a typo.

Code: Select all

echo $result[2]['fourth'];

//should be

echo $rows[2]['fourth'];