1

I'm using the following code to query my Firebird database, but the results do not include the column names, just numbers. It's my first time working with Firebird, I only worked with MySQL. Is there a way to get column names instead of numbers?

$dbh = ibase_connect($cdb, $this->session->userdata('client_username'), $this->session->userdata('client_password'), 'utf-8', '100');
$rid = ibase_query($dbh, $query);
$coln = ibase_num_fields($rid);
$dataArr = array();
while ($row = ibase_fetch_row ($rid)) {
    $dataArr[] = $row;
}

Result:

Array
(
    [0] => Array
        (
          [0] => 1
        )
 )

How can I get the column name that was [User_Id] instead of [0]? Is it possible?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
BRABO
  • 100
  • 7
  • It might be time to finally upgrade to PDO. You shouldn't be using old unmaintained APIs – Dharman Jul 20 '21 at 17:23
  • 2
    Have you tried fetching an associative array instead of numerical one? https://www.php.net/manual/en/function.ibase-fetch-assoc.php – Dharman Jul 20 '21 at 17:23
  • 2
    @Dharman That library isn't 'unmaintained', it just isn't part of the standard PHP distribution any more. It is currently maintained on https://github.com/FirebirdSQL/php-firebird – Mark Rotteveel Jul 20 '21 at 17:57

2 Answers2

1

As suggested by Dharman in the comments, you need to use ibase_fetch_assoc instead of ibase_fetch_row to get a result using column names (associative array).

As documented for ibase_fetch_assoc:

fetches one row of data from the result. If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using ibase_fetch_row() or use alias names in your query.

Compared to ibase_fetch_row:

Returns an array that corresponds to the fetched row, or false if there are no more rows. Each result column is stored in an array offset, starting at offset 0.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
-1

When you prepare a query Firebird returns the resultset information for it, including column datatypes and aliases. Use fbird_field_info(...) to query the alias.

Arioch 'The
  • 15,799
  • 35
  • 62