Page 1 of 1
getting data from an array
Posted: Fri Jan 05, 2007 5:54 am
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
Re: getting data from an array
Posted: Fri Jan 05, 2007 6:05 am
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.
Posted: Fri Jan 05, 2007 6:07 am
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.
Posted: Fri Jan 05, 2007 6:18 am
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
Posted: Fri Jan 05, 2007 6:24 am
by onion2k
Post your code and what error you're getting and we'll have a go at helping find a solution.
Posted: Fri Jan 05, 2007 6:44 am
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
Posted: Fri Jan 05, 2007 6:49 am
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,
Posted: Fri Jan 05, 2007 6:57 am
by hame22
thanks that is spot one!!
Posted: Fri Jan 05, 2007 9:07 am
by Kieran Huggins
Don't forget you can use print_r($row); to inspect your variables.. helps with debugging!
Posted: Fri Jan 05, 2007 9:12 am
by onion2k
Do not use impulse()'s code on a large (thousands of rows) result set. PHP won't like it.
Posted: Fri Jan 05, 2007 9:15 am
by hame22
Yeah I know, but this example is for use with just 5 rows - which is fine.
Thanks again for your help!
Posted: Fri Jan 05, 2007 9:20 am
by impulse()
What code would be ideal if the query returned 12,000 results?
Posted: Fri Jan 05, 2007 9:53 am
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.