How can I get the result of mysql_fetch_array to skip a row?
June 27, 2009 5:25 PM   Subscribe

Tearing my hair out over what feels like a simple PHP/SQL question: mysql_fetch_array returns, dur, an array of all the rows that matched your query. How can I tell it to skip a row of that array? More inside.

Basically, I'm fetching a bunch of rows for display. The FIRST of these rows completes a row in am html table. ANY OTHER ROW that it gets will be in a new table row.

I catch it like so:

$query_lots="SELECT * FROM lots_new WHERE lot_sku_id='$curr_key'";

$lots_result=mysql_query($query_lots) or die(mysql_error());

So, I need to say "With Row 1, do this. If there are other Rows, do that".

So, how can I say: $lots_result START WITH YOUR SECOND ROW OF RESULTS.

I cannot find an answer to this anywhere and I am going craaaaaazy. Thoughts/help?!
posted by GilloD to Technology (14 answers total)
 
Also "LIMIT" won't help because I NEED the first result.
posted by GilloD at 5:26 PM on June 27, 2009


Here's the detailed I version I asked over at PHPFreaks. Ugggggggggh

http://www.phpfreaks.com/forums/index.php/topic,258392.0.html
posted by GilloD at 5:34 PM on June 27, 2009


$query_lots="SELECT * FROM lots_new WHERE lot_sku_id='$curr_key'";

$lots_result=mysql_query($query_lots) or die(mysql_error());

$row1 =  mysql_fetch_array($lots_resut, MYSQL_NUM)) ;
if( $row1 ) {
  //process row 1, if any

  // process subsequent rows, if any
  while ($row = mysql_fetch_array($lots_resut, MYSQL_NUM)) { //
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
  }
}
But better, make your process handle rows uniformly, by making the single while do something like this:
$query_lots="SELECT * FROM lots_new WHERE lot_sku_id='$curr_key'";

$lots_result=mysql_query($query_lots) or die(mysql_error());

 // process ALL rows, if any
  while ($row = mysql_fetch_array($lots_resut, MYSQL_NUM)) { //
    printf("ID: %s  Name: %s", $row[0], $row[1]);
    //print row close html, followed immeditely by opening a new row:
    printf("</tr> <tr&gt");  
  }
}
//terminate the last row:
printf("</tr>");
And to make sure this works correctly, you need an ORDER BY on your query.
posted by orthogonality at 5:47 PM on June 27, 2009


I would (from memory)


$newarray = array_chunk($lots_result, 1, true);

should give you two arrays split on the first element.

then $newarray[0] should be your header row

$newarray[1] the stuff for the other table
posted by mattoxic at 5:48 PM on June 27, 2009


mattoxic, I'm no php guru, by I don't think $lots_result is an array -- it's (basically) a pointer ro a resut set, which can be (perhaps uni-directionally) iterated over.
posted by orthogonality at 5:53 PM on June 27, 2009


Iterate over the array, check if you're on the first iteration, if so, process your code for the first row then use a next statement to skip the remaining code. Leave the remaining code for your 2nd row and on.
posted by wongcorgi at 5:58 PM on June 27, 2009


Or just run mysql_fetch_array on your result set once which will increment the internal pointer. Then continue your while loop which will start on the 2nd row.

$lots_result = ...
$first_row_values = mysql_fetch_array($lots_result))

while ($row = mysql_fetch_array($lots_result)) {
... 2nd row -> nth row
}
posted by wongcorgi at 6:04 PM on June 27, 2009


I am in the company of genius. Thanks guys. I'm gonna work on this and get back to you
posted by GilloD at 6:06 PM on June 27, 2009


Oh yeah, I was supposing that mysql_fetch_array is called on the result. Then splitting the resulting array is more elegant than iterating
posted by mattoxic at 6:08 PM on June 27, 2009


mattoxic: the array returned from mysql_fetch_array() is only a single row. Splitting that array wouldn't do much.
posted by wongcorgi at 6:13 PM on June 27, 2009


seconding wongcorgi's answer
posted by soma lkzx at 6:15 PM on June 27, 2009


mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

Quite right, sorry
posted by mattoxic at 6:19 PM on June 27, 2009


Where does the value of $curr_key come from? You're using mysql_escape_string( $curr_key ) to prevent SQL injection attacks, right?
posted by ixohoxi at 7:51 PM on June 27, 2009


Ix- Yep. $curr_key is the Primary Key of another table. The Lots have to be linked to the items which have to be linked to the order data and there's an unknown number of each. Ughhh.
posted by GilloD at 7:43 AM on June 28, 2009


« Older The most breathable shoes you've ever worn.   |   Does additional testosterone, post-puberty, alter... Newer »
This thread is closed to new comments.