Page 1 of 1
Pull result set from mysql into an array
Posted: Mon Jul 24, 2006 10:57 am
by kbrown3074
Jcart | Please use 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
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]
Posted: Mon Jul 24, 2006 11:04 am
by John Cartwright
There is no parse error in that code.
Posted: Mon Jul 24, 2006 11:11 am
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.
Posted: Mon Jul 24, 2006 11:11 am
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'];
}
Posted: Mon Jul 24, 2006 11:26 am
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

Posted: Mon Jul 24, 2006 11:31 am
by kbrown3074
feyd | Please use 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
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]
Posted: Mon Jul 24, 2006 11:33 am
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";
Posted: Mon Jul 24, 2006 11:36 am
by kbrown3074
Thanks Everah..that was it. Told ya I had the brain farts going on today

Posted: Mon Jul 24, 2006 11:38 am
by RobertGonzalez
Glad I could help.
Posted: Mon Jul 24, 2006 1:03 pm
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.