Pull result set from mysql into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Pull result set from mysql into an array

Post by kbrown3074 »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to get a quick 1-dim array of the data from a mysql query.  It's only 1 field that I'm querying but I have had a 'brain-fart' all day today.  Below is what I have..that isnt working.  The error I am getting is "parse error, unexpected ']'"

Code: Select all

$query="select state from states";
$result=mysql_query($query,$db);
while ($myrow = mysql_fetch_assoc($query))
{
   $starray[] = $myrow;
}

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

There is no parse error in that code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this and see if you are getting any other errors. Also, make sure to look just above this code (or just above the line that you are getting the error on) because the code you posted looks clean at first.

Code: Select all

<?php
$query = 'SELECT `state` FROM `states`';
if (!$result = mysql_query($query,$db))
{
    die('Could not get the state list: ' . mysql_error());
}

while ($myrow = mysql_fetch_array($result))
{
   $starray[] = $myrow;
} 
?>
I also noticed you were trying to use mysql_fetch_assoc on your query, not the result. The error you got may be because you were trying to fill an array with non-existent mysql result.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

...although, shouldn't it look like this

Code: Select all

$query="select state from states";
$result=mysql_query($query,$db);
while ($myrow = mysql_fetch_assoc($query))
{
   $starray[] = $myrow['state'];
}
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

I would just put

Code: Select all

$result=mysql_query($query,$db) or die(MySQL_Error());
Same thing that Everah posted, just lesser lines and checks. That along with what Pimp said ;)
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Below is the code.  The line I am getting the error on is [b]$starray[] = $myrow['state'];[/b].  The get variables are populating just fine.

Code: Select all

<?
        $dbase=$_GET['dbase'];
        $edttable=$_GET['edttable'];
        $filename=$_GET['filename'];
        $site=$_GET['site'];
        header("Content-Type:  application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$filename.csv");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Expires: 0");


        if ($site == "A") { $pagehead = "ALL SITES COMBINED REPORT"; }
        if ($site == "B") { $pagehead = "BETHLEHEM COMBINED REPORT"; }
        if ($site == "F") { $pagehead = "ALTAMONTE SPRINGS COMBINED REPORT"; }
        if ($site == "T") { $pagehead = "DALLAS-FT. WORTH COMBINED REPORT"; }

        print("<strong>$pagehead</strong><br><br>\n");

        $dbcon = mysql_connect("localhost", "test", "test");
        $query="select * from util.states;
        $result=mysql_query($query,$dbcon) or die (mysql_error());
        while ($myrow = mysql_fetch_assoc($result))
        {
           $starray[] = $myrow['state'];
        }

        mysql_select_db($dbase,$dbcon);
        $export = "select distinct agentno,pa_cq,last,first from $edttable order by last,first";

        $expresult = mysql_query($export,$dbcon) or die (mysql_error());
        $num_fields = mysql_num_fields($expresult);

        print("<table border='1'>\n");
        print("<tr><td bgcolor=\"#FFFFCC\">License #</td><td bgcolor=\"#FFFFCC\">Last Name</td><td bgcolor=\"#FFFFCC\">First Name</td>\n");

      //print out the starray for headers
        for$(i=0; $i < count($starray); $i++)
        {
           print("<td bgcolor=\"#FFFFCC\">$starray[$i]</td>");
        }
        print("</tr>\n");

        while($dbrow = mysql_fetch_array($expresult))
        {
           print("<tr>");
           $pacq = ''; if($dbrow['pa_cq'] != '') { $pacq = $dbrow['pa_cq']; }
           $tempag = ''; if($dbrow['agentno'] != '') { $tempag = $dbrow['agentno']; }
           $first = ''; if($dbrow['first'] != '') { $first = $dbrow['first']; }
           $last = ''; if($dbrow['last'] != '') { $last = $dbrow['last']; }
           print("<td>$pacq</td><td>$last</td><td>$first</td>");

           $query = "select agentno,state,peoples,fraz_vet,fraz_ppl,stonebrdg,aflac,mutual,monumental,genworth,gwl,gwm from agstate where agentno='$tempag'";
           $result=mysql_query($query,$dbcon) or die (mysql_error());
           while ($myrow = mysql_fetch_array($result))
           {
               $state = ''; if($myrow['state'] != '') { $state = $myrow['state']; }
               // NEED TO PRINT OUT EACH STATE AS A COLUMN SOMEHOW
               print("<td>$state</td>");
            }
           print("</tr>\n");
        }
        print("</table>\n");

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by kbrown3074 on Mon Jul 24, 2006 11:33 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No closed quote on this line...

Code: Select all

$query="select * from util.states;
Make it this...

Code: Select all

$query="select * from util.states";
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

Thanks Everah..that was it. Told ya I had the brain farts going on today :?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Oh the joys of syntax highlighting.. have a good in our Favorite Editor thread and pick up an editor that can highlight your code.
Post Reply