开发者

PHP Get row 'id' from MySQL query results

开发者 https://www.devze.com 2023-02-12 03:18 出处:网络
I read somewhere (sorry I forgot where), that there is a way to associate an index number to rows fetched from an MySQL query result using a native PHP functi开发者_如何学Con (the person was not sure

I read somewhere (sorry I forgot where), that there is a way to associate an index number to rows fetched from an MySQL query result using a native PHP functi开发者_如何学Con (the person was not sure about the syntax so he didn't wrote it). For example:

The usual way:

$count = 1;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo "<li> ". $count ."id: " . $row['id'] . " lon: " . $row['lon']. ", lat: ". $row['lat'].", ". $row['road']. " </li>";
    $count++;
}

I know that thinking of this other way is not that important, but I'm just curious if there really is a function for that purpose.


you can add rownum to your results array, if you edit your sql like

SELECT @rownum:=@rownum+1 as rownum, p.* FROM MYTABLE p , (SELECT @rownum:=0) r 

so the loop like

    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        echo "<li> ". $row['rownum'] ."id: " . $row['id'] . " lon: " . $row['lon']. ", 
        lat: ". $row['lat'].", ". $row['road']. " </li>";

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消