Problem with getting all info from database query. (PHP & mySQL)

Discussion in 'OT Technology' started by GotVtec, Jun 10, 2003.

  1. GotVtec

    GotVtec 8th World Wonder

    Joined:
    Mar 14, 2000
    Messages:
    575
    Likes Received:
    0
    Location:
    Omaha, NE
    Here is snipet of my code:
    Code:
    $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:
    foreach(array($fn, $ln, $bn, $ht, $cs, $yg) as $alumni) {
    	print "$alumni\n";
    }
    
    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!
     
  2. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    You need to loop through your record set. Something like this:

    PHP:

    $query 
    'SELECT * FROM mytable';
    $result mysql_query($query);
    while(
    $row mysql_fetch_array($result))
      {
      echo 
    $row['column1'] . $row['column2'];  // yadda yadda yadda
      
    }  // end while($row = mysql_fetch_array($result))

    // if you got to here, 2 things may have happened:
    // 1.  You dished out all your data
    // 2.  There was no data to dish out (empty record set)
    // If you're fancy, you might do this too:

    if(mysql_num_rows($result) == 0)
      echo 
    'Dude, there\'s no data!';

    Thats keeping it pretty simply. I'd recommend some error checking in there in case you happen to write a funky query (mysql_error() is pretty handy).

    But that while loop is what you want to look at.
     
  3. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    dude...like woah!

    hehe, just had to :)

    |eb_CRX
     

Share This Page