我需要在 PHP 中對(duì) SQL 結(jié)果進(jìn)行以下排程:
if (!$result) { echo "An error occurred.\n"; exit; } while($array = pg_fetch_array($result)) { $list = $array['list']; } pg_free_result($result);
但只有 pedro
在陣列中傳回。
您需要 pg_fetch_assoc() 來(lái)取得傳回的關(guān)聯(lián)數(shù)組。
並且您需要建立一個(gè)陣列來(lái)附加節(jié)點(diǎn):
$lists = []; while ($row = pg_fetch_assoc($result)) { $lists[] = $row['list']; }
或如果你只是想玩一下,可以使用 pg_fetch_all_columns()。
https://www.php.net /manual/en/function.pg-fetch-all-columns.php
$lists = pg_fetch_all_columns($result, 0);
0 表示行中的第一列。