Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 06:43 AM
Ched
Guest
 
Posts: n/a
Default 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.
  #2  
Old July 17th, 2005, 06:43 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: Assigning values to arrays

Ched wrote:[color=blue]
> 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 */[/color]

Why??? You're not using the isolated variables ...
If $row is an array like
array('id'=>3, 'name'=>'pedro', 'email'=>'hexkid@hotpop.com')

that extract() call will create
$id = 3;
$name = 'pedro';
$email = 'hexkid@hotpop.com';
[color=blue]
> $array0[$i] = $row[0]; /* 1st Field of Row */[/color]

You set $i to 1; arrays usually start at 0.
[color=blue]
> $array1[$i] = $row[1]; /* 2nd Field of Row */
> $array2[$i] = $row[2]; /* 3rd Field of Row */
> $i++;
> }
></snip>
>
> Should this work?[/color]

Yes.
[color=blue]
> I am getting unexpected results when I try this.[/color]

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 :
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles