php - Get first entry from loop / while -
i need first id loop.
loop:
while ($row = $db->sql_fetchrow($result)) { echo ($row['post_id']) . '<br /><br />'; }
echo result:
817856<br /><br />817865<br /><br />817870<br /><br />817871<br /><br />817873<br /><br />817874<br /><br />
in case need number 817856
var_dump
var_dump($row['post_id']);
var_dump result:
string(6) "817856" string(6) "817865" string(6) "817870" string(6) "817871" string(6) "817873" string(6) "817874"
i tried:
reset , array_shift without success. thank you
you need use variable track loop , first id while looping. default have assign variable true , id inside loop make variable false variable stay false. using if condition ok.
$flag = true; while ($row = $db->sql_fetchrow($result)){ if($flag === true){ $id = $row['post_id']; $flag = false; } echo ($row['post_id']) . '<br /><br />'; } echo $id; // 817856
if need first id shorter version. cause query returns first row when don't use loop.
$row = $db->sql_fetchrow($result); echo $row['post_id']; //817856
Comments
Post a Comment