467,104 Members | 1,044 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

Assigning values to arrays

Hi,

I am performing a MySQL SELECT (which returns multiple rows) to
$result and then extracting the results with
mysql_fetch_array($result).
I then want to build a number of arrays using the fields of each row.
Here is the relevant code:
<snip>
$result =
$i = 1;
while ($row = mysql_fetch_array($result))
{
extract($row); /* break the row into its fields */
$array0[$i] = $row[0]; /* 1st Field of Row */
$array1[$i] = $row[1]; /* 2nd Field of Row */
$array2[$i] = $row[2]; /* 3rd Field of Row */
$i++;
}
</snip>

Should this work? I am getting unexpected results when I try this.

Thanks.
Jul 17 '05 #1
  • viewed: 1896
Share:
1 Reply
Ched wrote:
I am performing a MySQL SELECT (which returns multiple rows) to
$result and then extracting the results with
mysql_fetch_array($result).
I then want to build a number of arrays using the fields of each row.
Here is the relevant code:
<snip>
$result =
$i = 1;
while ($row = mysql_fetch_array($result))
{
extract($row); /* break the row into its fields */
Why??? You're not using the isolated variables ...
If $row is an array like
array('id'=>3, 'name'=>'pedro', 'email'=>'h*****@hotpop.com')

that extract() call will create
$id = 3;
$name = 'pedro';
$email = 'h*****@hotpop.com';
$array0[$i] = $row[0]; /* 1st Field of Row */
You set $i to 1; arrays usually start at 0.
$array1[$i] = $row[1]; /* 2nd Field of Row */
$array2[$i] = $row[2]; /* 3rd Field of Row */
$i++;
}
</snip>

Should this work?
Yes.
I am getting unexpected results when I try this.


What did you expect?
What happens differently?

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.