开发者

Read the first item of a PHP array

I have a PHP array generated by the following code:

$sql = "SELECT adresa.Id_Adresa as id FROM adresa";
$result = $this->os->db->conn->query($sql);
$data = array();
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
    $data[] = $r;
}

I want to grab the very first id of this array. How can I do that without parsing all the items? Than开发者_如何学编程ks in advance.


When you say first id of this array I am assuming that is the $data array, just do:

print_r($data[0]);


Why don't you limit the number of items returned if you don't need them?

$sql = "SELECT adresa.Id_Adresa as id FROM adresa LIMIT 1";


$sql = "SELECT adresa.Id_Adresa as id FROM adresa LIMIT 1";
$result = $this->os->db->conn->query($sql);
$firstId = $result->fetchColumn(0);

See the docs at http://de2.php.net/manual/en/pdostatement.fetchcolumn.php

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜