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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.