getting data from an array
Moderator: General Moderators
getting data from an array
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
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
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:hame22 wrote:I have an array $result generated from a query. It contains 5 records from my database consisting of 5 fields.
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.
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.
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
$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:
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.- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact: