Mysqli fetch_assoc first result

mysqli fetch assoc
pdo::fetch_assoc
mysqli fetch array
mysqli fetch all
mysqli fetch row
mysqli fetch single row
mysqli query
mysqli result

I have a fetch_assoc query that will grab 14 rows, but I want the FIRST result displayed so it is differnet to the others, then the rest are just ordinary.

    $site_events = $db->query("SELECT * FROM site_events ORDER BY time ASC limit 14");
        while($events = $site_events->fetch_assoc()) {

        if( first result???? ) { //this is where im stuck
           echo $events['title'], 'FIRST RESULT';
        }

        //then display others like a normal list..
        echo $events['title'], '<br />';
   }

You can fetch first line outside the "while" and then continue normally. But first you should probably check if there are data selected just in case:

$site_events = $db->query("SELECT * FROM site_events ORDER BY time ASC limit 14");
if($db->field_count){
   $event = $site_events->fetch_assoc();
   echo $event['title'], 'FIRST RESULT';

   while($events = $site_events->fetch_assoc()) {

     //then display others like a normal list..
     echo $events['title'], '<br />';
   }
}

mysqli_result::fetch_assoc - Manual, mysqli_result::fetch_assoc -- mysqli_fetch_assoc — Fetch a result row as an style only: A result set identifier returned by mysqli_query(), mysqli_store_result() or the primary index (code assumes primary index is the first field in the table): Definition and Usage. The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array. Note: Fieldnames returned from this function are case-sensitive.

I based mine on Yatin Trivedi's answer, but removed the increment with a variable which I unset. You wont notice the difference, but this is a bit faster. This is because the $i++ doens't have to be called every itteration. Also, unset() is really fast. Edit: That answer started with $i=0 and a $i++, now it doesnt anymore :)

$hasNotLooped = true; // set it before you run the loop
$site_events = $db->query("SELECT * FROM site_events ORDER BY time ASC limit 14");
    while($events = $site_events->fetch_assoc()) {

    if( $hasNotLooped ) {
       echo $events['title'], 'FIRST RESULT';
       unset($hasNotLooped);
    }
    //then display others like a normal list..
    echo $events['title'], '<br />';
}

PHP mysqli fetch_assoc() Function, Example - Object Oriented style. Fetch a result row as an associative array: <? php $mysqli = new mysqli("localhost� While mysql_result is a generally terrible function, it was useful for fetching a single result field *value* from a result set (for example, if looking up a user's ID). The behavior of mysql_result is approximated here, though you may want to name it something other than mysqli_result so as to avoid thinking it's an actual, built-in function.

$tmp=true;
$site_events = $db->query("SELECT * FROM site_events ORDER BY time ASC limit 14");
    while($events = $site_events->fetch_assoc()) {

    if( $tmp) { //this is where im stuck
       echo $events['title'], 'FIRST RESULT';
       $tmp=false;
    }
    else
    {
    //then display others like a normal list..
    echo $events['title'], '<br />';
    }
}

MySQL :: MySQL PHP API :: 3.11.5 mysqli_result::fetch_assoc , array mysqli_fetch_assoc(mysqli_result result);. Returns an associative array that corresponds to the fetched row or NULL if there are no more rows. Note. Field� A PHP result object (of the class mysqli_result) represents the MySQL result, returned by the SELECT or, DESCRIBE or, EXPLAIN queries. The mysqli_fetch_assoc() function accepts a result object as a parameter and, retrieves the contents of current row in the given result object, and returns them as an associative array.

mysqli_fetch_assoc, mysqli_free_result($result); ?> See also mysqli_fetch_array(), mysqli_fetch_row() and mysqli_fetch_object(). Prev � Home� Syntax mysqli_fetch_assoc(result); Definition and Usage. It is used to fetches a result row as an associative array. Return Values. It returns an associative array of strings representing the fetched row.

php fetch_assoc Code Example, 20. $result->free();. 21. } 22. . 23. /* close connection */. 24. $mysqli->close();. 25 ?> php fetch_assoc. sql by SISO on Dec 29 2019 Donate. 0. Description array mysqli_fetch_assoc ( resource result). Returns an associative array that corresponds to the fetched row or FALSE if there are no more rows.. The mysqli_fetch_assoc() function is used to return an associative array representing the next row in the result set for the result represented by the result parameter, where each key in the array represents the name of one of the result

reset counter to first row mysqli fetch_assoc(), $result_prices = $conn -> query($sql) or die(mysqli_error());. i use.. while ($row = $result_prices -> fetch_assoc()) { ..to get the rows one after� Description. Object oriented style public array mysqli_result::fetch_assoc (); Procedural style array mysqli_fetch_assoc (mysqli_result result); Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

Comments
  • you can always use a variable to store the state
  • Why not just if ($hasNotLooped) $hasNotLooped = false;?
  • Doesnt really matter I guess... Either way has advantages. You could say I just removed some info from the memory
  • Is it possible to make it so the first results inst duplicated again? Cause its duplicated and echos like a normal one as well.
  • Just wrap it with an else{}
  • Booleans are quicker than integers. Just saying.
  • you could improve speed by adding the $tmp++ in the if statement... (microscopical difference :) )
  • Is it possible to make it so the first results inst duplicated again? Cause its duplicated and echos like a normal one as well.