View Single Post
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: php query headers

 
0
  #3
May 6th, 2008
Ok, i think I answered my own question, someone please tell me if this was the wrong way to do this, but it works.
  1. // open connection
  2. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  3.  
  4. // select database
  5. mysql_select_db($db) or die ("Unable to select database!");
  6.  
  7. // create query
  8. $query = "SELECT * FROM people ORDER BY fname ASC";
  9.  
  10. // execute query
  11. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  12.  
  13. // see if any rows were returned
  14. if (mysql_num_rows($result) > 0) {
  15. // yes
  16. // print them one after another
  17. echo "<table cellpadding=10 border=0>";
  18. //I ADDED THE ECHO'S BELOW TO CREATE MY TABLE HEADINGS
  19. //Before the "While" statement displays the headings just once at the top of the page.
  20. echo "<tr>";
  21. echo "<td><strong>First Name</strong></td>";
  22. echo "<td><strong>Last Name</strong></td>";
  23. echo "<td><strong>Phone Number</strong></td>";
  24. echo "<td><strong>Extension</strong></td>";
  25. echo "<td><strong>Title</strong></td>";
  26. echo "<td><strong>Department</strong></td>";
  27. echo "<td><strong>Fax Number</strong></td>";
  28. echo "</td>";
  29. while($row = mysql_fetch_row($result)) {
  30. echo "<tr>";
  31. echo "<td>".$row[1]."</td>";
  32. echo "<td>".$row[2]."</td>";
  33. echo "<td>".$row[3]."</td>";
  34. echo "<td>".$row[4]."</td>";
  35. echo "<td>".$row[5]."</td>";
  36. echo "<td>".$row[6]."</td>";
  37. echo "<td>".$row[7]."</td>";
  38. echo "</tr>";
  39. }
  40. echo "</table>";
  41. }
  42. else {
  43. // no
  44. // print status message
  45. echo "No rows found!";
  46. }
  47.  
  48. // free result set memory
  49. mysql_free_result($result);
  50.  
  51. // close connection
  52. mysql_close($connection);
  53.  
  54. ?>
I added my heading tags between my open table tag and my while statement. This displays the headings only once at the top of the page. This solved my problem, but is it the correct way to do this. Pointers would be appreciated.

Thanks,
Reply With Quote