getting data from an array

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

getting data from an array

Post by hame22 »

Hi

I have an array $result generated from a query. It contains 5 records from my database consisting of 5 fields.

I am trying to extract some fields from each record held in the array.

How would I go about doing this

I have tried

$job1 = $result['0']['job_title'];
$job2 = $result['1']['job_title'];

but is obviously not the correct method!

I would appreciate if anyone could help

thanks in advance
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: getting data from an array

Post by onion2k »

hame22 wrote:I have an array $result generated from a query. It contains 5 records from my database consisting of 5 fields.
No you don't. A result set from a database query is not an array, it's a reference to the results. You need to use one of the many database functions to go through the results and get the data. These functions (assuming you're on MySQL) are:

mysql_fetch_array()
mysql_fetch_assoc()
mysql_fetch_row()
mysql_fetch_object()

There's also mysql_result() but it's horribly slow and should never be used.

Read the PHP manual pages for details about how to use each of these functions. There's plenty of examples in the comments.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

mysql_query returns a result resource (not an array) that needs to be passed to one of the resource handling functions. Have a look at mysql_fetch_array.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

ok so if i use $row = mysql_fetch_array($result)

how do i then get the individual variables from $row ??

thanks for your help
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Post your code and what error you're getting and we'll have a go at helping find a solution.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

I have done this

$row = mysql_fetch_array($result);

but the array is only beingmade up of one record.

I could do
while($row = mysql_fetch_array($result)
{
}


but I would like to set up indiviudal varibales

so $job1 = record 1 of query result, $job2 = record 2 of query result .......

any ideas I'm a little stuck, thanks for you time
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Code: Select all

$mysqlQuery = mysql_query("SELECT something FROM somewhere");

$i = 0;
while ($results = mysql_fetch_array($mysqlQuery)) {
  $firstName[$i] = $results['<fieldName>'];
  $surname[$i] = $results['<fieldName2>'];
  $i++;
}

echo $firstName[0]; # This will echo the first value that was retrieved.
Hope that helps,
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thanks that is spot one!!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Don't forget you can use print_r($row); to inspect your variables.. helps with debugging!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Do not use impulse()'s code on a large (thousands of rows) result set. PHP won't like it.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

Yeah I know, but this example is for use with just 5 rows - which is fine.

Thanks again for your help!
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

What code would be ideal if the query returned 12,000 results?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

impulse() wrote:What code would be ideal if the query returned 12,000 results?
Anything that doesn't copy the whole lot into set of a PHP arrays.
Post Reply