NYCPHP Meetup

NYPHP.org

[nycphp-talk] RE: Best way to read in an array

Eric Rank erank at isthmus.com
Fri Mar 12 15:13:56 EST 2004


One of my favorite was of doing this is a one liner.

After you get the $result from the query...

for($i=0; $i<mysql_num_rows($result); $allrows[$i++] =
mysql_fetch_assoc($result));


$allrows would look like this for a result with 3 rows:
Array
(
    [0] => Array
        (
        [firstcolname] => value of first col in FIRST row
        [secondcolname] => value of second col in FIRST row
        [thirdcolname] => value of third col in FIRST row        )    [1] =>
Array
        (
        [firstcolname] => value of first col in SECOND row
        [secondcolname] => value of second col in SECOND row
        [thirdcolname] => value of third col in SECOND row        )    [2]
=> Array        (        [firstcolname] => value of first col in THIRD row
[secondcolname] => value of second col in THIRD row        [thirdcolname] =>
value of third col in THIRD row        ))Of course, if you want to do
anything with the information, you can just put it within the for loop. This
works well for situations when you need to use the result later on or for
more than one thing.E.----- Original Message ----- From: "Chris Bielanski"
<Cbielanski at inta.org>
To: "'NYPHP Talk'" <talk at lists.nyphp.org>
Sent: Friday, March 12, 2004 12:57 PM
Subject: RE: [nycphp-talk] RE: Best way to read in an array


> let me modify that a little...
>
> WAS:
> array_push($plotdata,
> array($row['category'],$row['month1'],$row['month2'],$row['month3']));
>
> SHOULD BE:
> array_push($plotdata,
>  array('category' => $row['category'],'month1' => $row['month1'], 'month2'
> => $row['month2'], 'month3' =>$row['month3']));
>
> Sorry about that!! :)
>
> ~C
>
> > -----Original Message-----
> > From: Chris Bielanski [mailto:Cbielanski at inta.org]
> > Sent: Friday, March 12, 2004 1:54 PM
> > To: 'NYPHP Talk'
> > Subject: RE: [nycphp-talk] RE: Best way to read in an array
> >
> >
> > do your query, then with the $result...
> >
> > while($row = mysql_fetch_assoc($result))
> > {
> > // this will need to be put into an array if you get multiple
> > rows, so...
> > array_push($plotdata,
> > array($row['category'],$row['month1'],$row['month2'],$row['month3']));
> > }
> > Bear in mind that column names are case-senseitive from the
> > query to the
> > associative index name. When using select *, the indices in
> > $row will be
> > *exactly* as the column names are in the table.
> >
> > HTH
> > ~Chris
> >
> > > -----Original Message-----
> > > From: Rafi Sheikh [mailto:Rafi.Sheikh at Ingenix.com]
> > > Sent: Friday, March 12, 2004 1:44 PM
> > > To: 'talk at lists.nyphp.org'
> > > Subject: [nycphp-talk] RE: Best way to read in an array
> > >
> > >
> > > Hello List:  I am very new and hoping to get some help in
> > > terms of reading
> > > an array (I have tried but failing)
> > >
> > > BASIC: apache 1.3.29, PHP 4.3.4, MySQL 4.0.17-nt on WIN2K-Pro
> > >
> > > I have data as :
> > > category month1 month2 month3
> > > abc 100 99 100
> > > xyz 98 99 89
> > >
> > > etc...
> > >
> > > QUESTION:
> > > I need to geteach row in in variable so I could use it in a
> > > line graph or
> > > as:
> > >
> > > $datax=array(100,99,100)   //to be laid out, I will use
> > > $datax in my graph
> > > plot line
> > >
> > > There would be 6 rows that I need 6 lines.
> > >
> > > I have the table in a table in Mysql
> > >
> > > PROBLEM:
> > > I have no idea how can I read the table so each row is assigned to a
> > > varaible that I oculd use as example above.
> > >
> > > I have read and re-read arcticles, but I am missing
> > > things..any help in
> > > formulating this block is truly appreciated.
> > >
> > > Regards,
> > >
> > > Rafi
> > >
> > >
> > > This e-mail, including attachments, may include confidential and/or
> > > proprietary information, and may be used only by the person
> > > or entity to
> > > which it is addressed. If the reader of this e-mail is not
> > > the intended
> > > recipient or his or her authorized agent, the reader is
> > > hereby notified that
> > > any dissemination, distribution or copying of this e-mail is
> > > prohibited. If
> > > you have received this e-mail in error, please notify the
> > > sender by replying
> > > to this message and delete this e-mail immediately.
> > > _______________________________________________
> > > talk mailing list
> > > talk at lists.nyphp.org
> > > http://lists.nyphp.org/mailman/listinfo/talk
> > >
> > _______________________________________________
> > talk mailing list
> > talk at lists.nyphp.org
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list