我需要在 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
在數(shù)組中返回。
您需要 pg_fetch_assoc() 來(lái)獲取返回的關(guān)聯(lián)數(shù)組。
并且您需要建立一個(gè)數(shù)組來(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 表示行中的第一列。