943,172 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 57
  • PHP RSS
Aug 30th, 2010
0

no output from result set

Expand Post »
OK, putting together a simple table based on the output from a sql query. mysql_num_fields() returns 9 so I know I'm getting ONE row back. mysql_field_name($result, $i) returns the column name.

When I try to output the table row(s), I get nothing... nada... zip... I must be missing something! (tired eye/brain syndrome?)

Here's the code:
PHP Syntax (Toggle Plain Text)
  1. the output can be found here: http://hoatime.com/buildgrid.php
  2. <?php
  3. include "inc/sqlinc.php";
  4. $table = "hoa_board";
  5.  
  6. // get all records
  7. $query = "SELECT * FROM $table WHERE 1";
  8.  
  9. // query the database
  10. $result = mysql_query($query) or die(mysql_error());
  11.  
  12. //see how many records/rows we got back
  13. $totalField = mysql_num_fields($result);
  14.  
  15. //print table/grid headers
  16. echo "<html><head></head><body>\n";
  17. echo "<center>\n";
  18. echo "<br>Tot Fields: $totalField<br>\n";
  19. echo "<table>\n";
  20. echo "<tr>\n";
  21.  
  22. echo "<th>Add</th>\n";
  23. echo "<th>Edit</th>\n";
  24. echo "<th>Delete</th>\n";
  25.  
  26. for($i = 0; $i < $totalField; $i++)
  27. {
  28. echo "<th>\n";
  29. echo (mysql_field_name($result, $i));
  30. echo "</th>\n";
  31. }
  32. echo "</tr>\n";
  33.  
  34. // Rewind the pointer on the result handle
  35. mysql_data_seek ($result, 0);
  36.  
  37. // print out the data rows
  38. while($row = mysql_fetch_assoc($result))
  39. {
  40. // start a new row
  41. echo "<tr>\n";
  42.  
  43. // send out the icons with links so the user can add/edit/delete records
  44. // url is formatted as: http://www.foo.com/form_name.php?action=type&rec_id=123
  45. echo " <td><a href='hoa_board.php?action=add&$row[$i]'><img src='images/addicon.gif'></a></td>\n";
  46. echo " <td><a href='hoa_board.php?action=edit&$row[$i]'><img src='images/editicon.gif'></a></td>\n";
  47. echo " <td><a href='hoa_board.php?action=del&$row[$i]'><img src='images/deleteicon.gif'></a></td>\n";
  48.  
  49. // now output the record for this row
  50. for($i = 0; $i < $totalField; $i++)
  51. {
  52. echo " <td>$row[$i]</td>\n";
  53. }
  54. // end the record for this row
  55. echo "</tr>\n";
  56. }
  57. // end the table
  58. echo "</table>\n";
  59. echo "</center>\n";
  60. echo "</body></html>\n";
  61. ?>
Similar Threads
Reputation Points: 12
Solved Threads: 8
Junior Poster
ppetree is offline Offline
157 posts
since Oct 2009
Aug 31st, 2010
0
Re: no output from result set
Click to Expand / Collapse  Quote originally posted by ppetree ...
OK, putting together a simple table based on the output from a sql query. mysql_num_fields() returns 9 so I know I'm getting ONE row back. mysql_field_name($result, $i) returns the column name.

When I try to output the table row(s), I get nothing... nada... zip... I must be missing something! (tired eye/brain syndrome?)

Here's the code:
PHP Syntax (Toggle Plain Text)
  1. the output can be found here: http://hoatime.com/buildgrid.php
  2. <?php
  3. include "inc/sqlinc.php";
  4. $table = "hoa_board";
  5.  
  6. // get all records
  7. $query = "SELECT * FROM $table WHERE 1";
  8.  
  9. // query the database
  10. $result = mysql_query($query) or die(mysql_error());
  11.  
  12. //see how many records/rows we got back
  13. $totalField = mysql_num_fields($result);
  14.  
  15. //print table/grid headers
  16. echo "<html><head></head><body>\n";
  17. echo "<center>\n";
  18. echo "<br>Tot Fields: $totalField<br>\n";
  19. echo "<table>\n";
  20. echo "<tr>\n";
  21.  
  22. echo "<th>Add</th>\n";
  23. echo "<th>Edit</th>\n";
  24. echo "<th>Delete</th>\n";
  25.  
  26. for($i = 0; $i < $totalField; $i++)
  27. {
  28. echo "<th>\n";
  29. echo (mysql_field_name($result, $i));
  30. echo "</th>\n";
  31. }
  32. echo "</tr>\n";
  33.  
  34. // Rewind the pointer on the result handle
  35. mysql_data_seek ($result, 0);
  36.  
  37. // print out the data rows
  38. while($row = mysql_fetch_assoc($result))
  39. {
  40. // start a new row
  41. echo "<tr>\n";
  42.  
  43. // send out the icons with links so the user can add/edit/delete records
  44. // url is formatted as: http://www.foo.com/form_name.php?action=type&rec_id=123
  45. echo " <td><a href='hoa_board.php?action=add&$row[$i]'><img src='images/addicon.gif'></a></td>\n";
  46. echo " <td><a href='hoa_board.php?action=edit&$row[$i]'><img src='images/editicon.gif'></a></td>\n";
  47. echo " <td><a href='hoa_board.php?action=del&$row[$i]'><img src='images/deleteicon.gif'></a></td>\n";
  48.  
  49. // now output the record for this row
  50. for($i = 0; $i < $totalField; $i++)
  51. {
  52. echo " <td>$row[$i]</td>\n";
  53. }
  54. // end the record for this row
  55. echo "</tr>\n";
  56. }
  57. // end the table
  58. echo "</table>\n";
  59. echo "</center>\n";
  60. echo "</body></html>\n";
  61. ?>
~_^ hey there ppetree,

nothin's seems wrong in the output here (http://hoatime.com/buildgrid.php)
what output are you expecting?

though the icons or the add, edit and delete functions are not working yet.
Last edited by ﻼim; Aug 31st, 2010 at 1:18 am. Reason: error. ~_^
Reputation Points: 10
Solved Threads: 13
Junior Poster
ﻼim is offline Offline
171 posts
since Aug 2010
Aug 31st, 2010
0
Re: no output from result set
Sorry, before I went to bed last night I made a change that I knew would work but that I cant use because I need access to the row names. The code that doesnt work is now posted and the url shows the problem.

I get 1 row back with 9 fields, I get table headers made up of the column names returned with the row. I get no data displayed in the actual table.
Reputation Points: 12
Solved Threads: 8
Junior Poster
ppetree is offline Offline
157 posts
since Oct 2009
Aug 31st, 2010
0
Re: no output from result set
Click to Expand / Collapse  Quote originally posted by ppetree ...
Sorry, before I went to bed last night I made a change that I knew would work but that I cant use because I need access to the row names. The code that doesnt work is now posted and the url shows the problem.

I get 1 row back with 9 fields, I get table headers made up of the column names returned with the row. I get no data displayed in the actual table.
that's weird.
just this morning i went over to the link and i saw the right outputs.
like the number of rows which was one (1)
then the nine (9) fields or column names
and lastly there were data in the table.

do you changed the code?
why is it that there are no more data when i visit again the site just a moment ago?

or you just emptied the table in the database?
Reputation Points: 10
Solved Threads: 13
Junior Poster
ﻼim is offline Offline
171 posts
since Aug 2010
Aug 31st, 2010
0
Re: no output from result set
The code posted above doesnt work and I cant see why. When you saw output that was from another piece of code.

In the code that did work I changed the while() as follows:
//  while($row = mysql_fetch_assoc($result))
  while($row = mysql_fetch_row($result))
  {
    // start a new row
    echo "<tr>\n";

    // send out the icons with links so the user can add/edit/delete records
    // url is formatted as: http://www.foo.com/form_name.php?action=type&rec_id=123
   	echo "  <td><a href='hoa_board.php?action=add&$row[0]'><img src='images/addicon.gif' border='0'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=edit&$row[0]'><img src='images/editicon.gif' border='0'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=del&$row[0]'><img src='images/deleteicon.gif' border='0'></a></td>\n";

    // now output the record for this row
    foreach ($row as $field)      	{
      echo "  <td>$field</td>\n";
    }
    // end the record for this row    
    echo "</tr>\n";
  }
Reputation Points: 12
Solved Threads: 8
Junior Poster
ppetree is offline Offline
157 posts
since Oct 2009
Aug 31st, 2010
0
Re: no output from result set
Hah! Found it! Talk about sleepy eyes... and apparently I wasn't the only one because three of the emails I received missed it as well!

while($row = mysql_fetch_assoc($result))

should be

while($row = mysql_fetch_array($result))

Because you can't reference an associative array using indexed array referencing methods (i.e. $row[$i]).

Sheesh... talk about feeling stoopid! LOL
Reputation Points: 12
Solved Threads: 8
Junior Poster
ppetree is offline Offline
157 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Android Market Web API
Next Thread in PHP Forum Timeline: calling Session?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC