SQL Query and Arrays [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

SQL Query and Arrays [Solved]

Post 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
Last edited by christh on Sun Sep 26, 2010 6:02 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: SQL Query and Arrays

Post 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.
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Re: SQL Query and Arrays

Post by christh »

Excellent - thank you John
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: SQL Query and Arrays [Solved]

Post 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'];
Post Reply