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:

the output can be found here: http://hoatime.com/buildgrid.php
<?php
include "inc/sqlinc.php";
$table = "hoa_board";

  // get all records
  $query = "SELECT * FROM $table WHERE 1";

  // query the database
  $result = mysql_query($query) or die(mysql_error());

 	//see how many records/rows we got back
 	$totalField = mysql_num_fields($result);
    	
 	//print table/grid headers
 	echo "<html><head></head><body>\n";
 	echo "<center>\n";
 	echo "<br>Tot Fields: $totalField<br>\n";
 	echo "<table>\n";
 	echo "<tr>\n";
 	
 	echo "<th>Add</th>\n";
 	echo "<th>Edit</th>\n";
 	echo "<th>Delete</th>\n";
 	
 	for($i = 0; $i < $totalField; $i++)
	{
	  echo "<th>\n";
	  echo (mysql_field_name($result, $i));
	  echo "</th>\n";	
	}
	echo "</tr>\n";

  // Rewind the pointer on the result handle
  mysql_data_seek ($result, 0);

  // print out the data rows
  while($row = mysql_fetch_assoc($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[$i]'><img src='images/addicon.gif'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=edit&$row[$i]'><img src='images/editicon.gif'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=del&$row[$i]'><img src='images/deleteicon.gif'></a></td>\n";

    // now output the record for this row    
 	  for($i = 0; $i < $totalField; $i++)
  	{
      echo "  <td>$row[$i]</td>\n";
    }
    // end the record for this row    
    echo "</tr>\n";
  }
  // end the table
 	echo "</table>\n";
 	echo "</center>\n";
 	echo "</body></html>\n";
?>

Recommended Answers

All 5 Replies

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:

the output can be found here: http://hoatime.com/buildgrid.php
<?php
include "inc/sqlinc.php";
$table = "hoa_board";

  // get all records
  $query = "SELECT * FROM $table WHERE 1";

  // query the database
  $result = mysql_query($query) or die(mysql_error());

 	//see how many records/rows we got back
 	$totalField = mysql_num_fields($result);
    	
 	//print table/grid headers
 	echo "<html><head></head><body>\n";
 	echo "<center>\n";
 	echo "<br>Tot Fields: $totalField<br>\n";
 	echo "<table>\n";
 	echo "<tr>\n";
 	
 	echo "<th>Add</th>\n";
 	echo "<th>Edit</th>\n";
 	echo "<th>Delete</th>\n";
 	
 	for($i = 0; $i < $totalField; $i++)
	{
	  echo "<th>\n";
	  echo (mysql_field_name($result, $i));
	  echo "</th>\n";	
	}
	echo "</tr>\n";

  // Rewind the pointer on the result handle
  mysql_data_seek ($result, 0);

  // print out the data rows
  while($row = mysql_fetch_assoc($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[$i]'><img src='images/addicon.gif'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=edit&$row[$i]'><img src='images/editicon.gif'></a></td>\n";
   	echo "  <td><a href='hoa_board.php?action=del&$row[$i]'><img src='images/deleteicon.gif'></a></td>\n";

    // now output the record for this row    
 	  for($i = 0; $i < $totalField; $i++)
  	{
      echo "  <td>$row[$i]</td>\n";
    }
    // end the record for this row    
    echo "</tr>\n";
  }
  // end the table
 	echo "</table>\n";
 	echo "</center>\n";
 	echo "</body></html>\n";
?>

~_^ 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.

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.

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.

:-O 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?

:X or you just emptied the table in the database?

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))
  [B]while($row = mysql_fetch_row($result))[/B]
  {
    // 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
    [B]foreach ($row as $field)    [/B]  	{
      echo "  <td>$field</td>\n";
    }
    // end the record for this row    
    echo "</tr>\n";
  }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.