Page 1 of 1

Problem with getting all info from database query.

Posted: Tue Jun 10, 2003 11:01 am
by immortalsix
Here is snipet of my code:

Code: Select all

$sql = "SELECT * FROM sda_alumni";
$sql_result = mysql_query($sql) or die ("could not query the database");
$row = mysql_fetch_array($sql_result);
	$fn = $rowї'first_name'];
	$ln = $rowї'last_name'];
	$bn = $rowї'bond_number'];
	$ht = $rowї'current_hometown'];
	$cs = $rowї'current_state'];
	$yg = $rowї'year_graduated'];

?>

<table border=1>
<tr>
	<td><b>First Name</b></td>
	<td><b>Last Name</b></td>
	<td><b>Bond Number</b></td>
	<td><b>Hometown</b></td>
	<td><b>State</b></td>
	<td><b>Year Graduated</b></td>
</tr>
<tr>
	<td><?php echo ($fn); ?></td>
	<td><?php echo ($ln); ?></td>
	<td><?php echo ($bn); ?></td>
	<td><?php echo ($ht); ?></td>
	<td><?php echo ($cs); ?></td>
	<td><?php echo ($yg); ?></td>
I know this code isn't correct, mainly because it doens't work, but I can't figure out how to get it to grab each record from the database.

I tried to do this and send the information into an array:

Code: Select all

foreach(array($fn, $ln, $bn, $ht, $cs, $yg) as $alumni) &#123;
	print "$alumni\n";
&#125;
But yet again, it would only output a single record from the database.

If you can not visualize what I am trying to do, you can see the single record output I have created at:
http://www.immortalsix.com/alumni.php

Thanks for any help!

Posted: Tue Jun 10, 2003 2:30 pm
by twigletmac
Try this:

Code: Select all

<?php
// specify exactly what columns you want returned in the SELECT
$sql = "SELECT first_name, last_name, bond_number, current_hometown, current_state, year_graduated FROM sda_alumni"; 

// use @ to suppress any default error messages if the SQL fails
@$sql_result = mysql_query($sql) or die ("could not query the database"); 

// do the beginning bit of the table:
?>
<table border=1> 
<tr> 
   <th>First Name</th> 
   <th>Last Name</th> 
   <th>Bond Number</th> 
   <th>Hometown</th> 
   <th>State</th> 
   <th>Year Graduated</th> 
</tr> 
<?php
// now loop through the results to get everything printed:
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr> 
   <td><?php echo $row['first_name']; ?></td> 
   <td><?php echo $row['last_name']; ?></td> 
   <td><?php echo $row['bond_number']; ?></td> 
   <td><?php echo $row['current_hometown']; ?></td> 
   <td><?php echo $row['current_state']; ?></td> 
   <td><?php echo $row['year_graduated']; ?></td> 
</tr>
<?php
} // end while

// finish the table
?> 
</table>
Mac