Jump to content

mysql_fetch_array()


dan2684

Recommended Posts

Hello all,

 

I need to get the first row of a result set at one part of my page and then all rows further down the page.

 

I only want to make one trip to the database so I thought I could fetch the result set and then display various different parts of it. The problem I'm having is, I'm using mysql_fetch_array() to display the first row but when I use the same function on the result set further down the page, the marker has moved showing all results from the second row onwards.

 

Enough rambling!

 

Basically, I do this:

 

$sql = "SELECT * FROM categories";
$result = mysql_query($sql);
$cat = mysql_fetch_array($result);
$catId = $_POST['catId'] ? $_POST['catId'] : $cat['id'];

 

And then this doesn't work properly:

 

while ($category = mysql_fetch_array($result)) {
    echo "<option value=\"{$category['id']}\">{$category['name']}</option>";
}

 

Any help would be greatly appreciated.

 

Thanks,

 

Dan

Link to comment
https://forums.phpfreaks.com/topic/237371-mysql_fetch_array/
Share on other sites

Get all the rows and add them to an array.

 

$categories = array();
while ($category = mysql_fetch_array($result)) {
  $categories[] = $category;
}

// get first row:  echo $categories[0];

// other rows
$row = 0;
foreach ($categories as $category) {
  if ($row > 0)
     echo $category;
  $row++;
}

Link to comment
https://forums.phpfreaks.com/topic/237371-mysql_fetch_array/#findComment-1219759
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.