I'm exeriencing a strange problem with pg_fetch_all
(postgresql
) : it never returns me more tan 2 columns
For exemple, this code :
$dbh = pg_connect("host=localhost dbname=dbname user=user password=passwd");
$query = "SELECT m.id, v.nom, v.id FROM machine m, version v WHERE m.id_version = v.id;";
$result = pg_query($dbh, $query);
$array = pg_fetch_all($result);
print_r($array);
Only prints me m.id
, v.nom
!
If I paste the exactly same SQL request in pg_my_admin, each colomn is returned.
If I parse my result as
$array = array();
while ($row = pg_fetch_row($result)) {
$array[] = $row;
}
it works perfectly. Why ?
It is because you have two columns with the same name (in two different tables). You need to use an alias like this :
$query = "SELECT m.id AS mId, v.nom, v.id AS vId FROM machine m, version v WHERE m.id_version = v.id;";
You will then be able to retrieve the column m.id
with the name mId
and v.id
with the name vId
You can find more info about alias in SQL here
The reason it works with pg_fetch_row() is because this one returns the result based on the index of the column on your query not as an associative array.