Page 1 of 1

Loop results from MySQL DB

Posted: Tue Nov 02, 2004 9:37 am
by senojeel
I think this is probably a simple thing to do, except I don't have good PHP chops yet.

I want to return a list of names from a MySQL db grouped by section, with the section being a header.

Example:

Violin I
Name One
Name Two
Name Three

Violin II
Name One
Name Two
Name Three

Viola
Name One
etc...

It would be great if I could get a starting point with the code. My fields are section, firstName, lastName. My table is members.

Thanks!
Shawn

Posted: Tue Nov 02, 2004 10:02 am
by timvw

Code: Select all

SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";

Code: Select all

<?php
$section = '';
while ($row = mysql_fetch_assoc($result)) {
  if ($section != $row['section'])
  {
     echo "<b>Section</b></br>";
     $section = $row['section'];  
  }
  echo $row['lastName'] . ' ' . $row['firstName'] . '</br>';
}
?>

Posted: Tue Nov 02, 2004 10:17 am
by senojeel
Thanks for you response timvw.

When I try to use that, I get a parse error: "Parse error: parse error, unexpected '{' in /Users/philharmonicindy/Sites/members/test1.php on line 70"

Line 70 in my page is the "{" in line 5.

Any ideas?

Posted: Tue Nov 02, 2004 10:22 am
by Draco_03
is your query is saved in $result ?

Code: Select all

$sql="SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";
$result = mysql_query($sql) or die(MySQL_error());
like that

Posted: Tue Nov 02, 2004 10:28 am
by senojeel
I had something like that. I inserted yours and still got the error. Here is my code.

Code: Select all

<?php
$connection = mysql_connect("$server","$username","$password");

$sql="SELECT * FROM members ORDER BY section ASC, lastName ASC, firstName ASC";
$result = mysql_query($sql) or die(MySQL_error());

$section = '';
while ($row = mysql_fetch_assoc($result)) &#123;
  if ($section != $row&#1111;'section'])
&#123;
	echo "<b>Section</b></br>";
	$section = $row&#1111;'section'];  
  &#125;
  echo $row&#1111;'lastName'] . ' ' . $row&#1111;'firstName'] . '</br>';
&#125;
?>

Posted: Wed Nov 03, 2004 4:42 pm
by senojeel
I got it to work. There must have been some gremlins in the code when I copy and pasted. So I keyed everything back in manually.

Thanks!
Shawn