I make a PHP request to the database to find a record where the code of the city of departure and the code of the city of arrival are equal to those requested by the user:

$query = "SELECT * FROM special_offers WHERE to_iata = '{$to_iata}' AND from_iata = '{$from_iata}'"; $result = pg_query($query) or die('Query failed: ' . pg_last_error()); 

Now I have only one record of suitable data in the database, but I cannot figure out how to get it right.

I process the data, trying to collect the values ​​of one particular key into an array:

 $rows = pg_fetch_assoc($result); foreach($rows as $row) { array_push($hashes, $row['data_hash']); } 

But in fact, it turned out that one record is not provided in the form of a nested array with a zero index, but simply in the form of a single array in the format key => value . How can I properly request data from a database so that an array of records will come to me, and not one record by an array? I tried to use pg_fetch_assoc() instead of pg_fetch_assoc() , but this did not change anything, only additional keys were added.

    1 answer 1

    There is a pg_fetch_all function pg_fetch_all returns exactly an array of all records.

    And pg_fetch_assoc , as indicated in the manual, returns one line of the result. To get the next one, you need to call pg_fetch_assoc again and so on until the function returns false . In general, as well as in other PHP libraries of access to databases.

     while ($row = pg_fetch_assoc($result)) { echo $row['id']; echo $row['author']; echo $row['email']; } 

    You are most likely looking at all for the pg_fetch_all_columns function which will immediately return a ready-made array of the values ​​of a specific column.