dan2684 Posted May 24, 2011 Share Posted May 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237371-mysql_fetch_array/ Share on other sites More sharing options...
gizmola Posted May 24, 2011 Share Posted May 24, 2011 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/237371-mysql_fetch_array/#findComment-1219759 Share on other sites More sharing options...
dan2684 Posted May 24, 2011 Author Share Posted May 24, 2011 Good point Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/237371-mysql_fetch_array/#findComment-1219763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.