Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rewind/reset mysql_fetch_field()

Status
Not open for further replies.

edpatterson

IS-IT--Management
Feb 24, 2005
186
Is there a way to 'rewind' the pointer used by mysql_fetch_field()?

I am trying to step through the results of a query once in 2 different functions.

Inaccurate example put shows the problem
$result = array('serial', 'property', 'mac')

function createTableHeader($result){
$table = "<table border = 1 cellpadding = 3><tr>\n";
while($field = mysql_fetch_field($result)){
$table = $table."<th>$field->name</th>";
}
$table = $table."</tr>\n";
return($table);
}
echo createTableHeader($result);
echo otherFunctionsHere();
echo createTableHeader($result);

The table with header is only printed once, the table is properly closed in one of the otherFunctionsHere();

Ideas?
 
I believe you could use this:

Code:
mysql_data_seek($result,0);
 
I would take a different approach. Instead of using fetch_field you could retrieve the rows with mysql_fetch_assoc() which puts them into an associative array keyed by column name.
With the first row retrieved call the header function you wrote and just iterate the keys of the row. That way you need not rewind and one mysql_*** function call will do what you want.
 
That is how my original (not shown) createTable() function worked. Here is what I am doing, maybe there is a better way.

A user enters a serial number, the number is used in a query. If the query has zero rows an add equipment page is generated. If it has a row the information is shown in a table.

The reason I am trying to read the query twice is to generate the header rown and a row of textboxes for data entry. I could probably populate an array and use it but it seems like that would be a waste of time/memory.

Thanks,
Ed
 
When you retrieve the row with mysql_fetch_array() you have already populated an array.
It appears that there will be one match or none.
Code:
$SQL = "SELECT * blah....";
$result = mysql_query($SQL) or die('MySQL said: '.mysql_error());
# check if there's a row
if (mysql_num_rows($result)>0)[
   $row = mysql_fetch_assoc($result);
   createTableHeader($row);
   # the array is already there
   # so you have access to $row['columname']
   etc.
 
To quote a beer comercial, "brilliant!"

I was generating the beginning of the table then checking for number of rows. Simple solution, check first.

DOH!

Thanks,
Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top