Results from firebird DB columns are nameless just numbers - php

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?

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.

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.
https://www.php.net/manual/en/function.ibase-prepare.php
https://www.php.net/manual/en/function.ibase-field-info.php

Related

remove extra column of data gotten from pdo [duplicate]

I have a script that is outputting to a CSV file. However, even though there is currently one row in the database, the output I'm getting is echoing out each column from each row in the table twice.
For example:
1,1,John,John,Smith,Smith,2014,2014
Should be
1,John,Smith,2014
This worked fine before I went with PDO and prepared statements, so I'm thinking maybe I'm not understanding how fetch() works correctly.
Below is my code. Any idea what I could be doing wrong?
// get rows
$query_get_rows = "SELECT * FROM Contacts ORDER BY date_added DESC";
$result_get_rows = $conn->prepare($query_get_rows);
$result_get_rows->execute();
$num_get_rows = $result_get_rows->rowCount();
while ($rows_get_rows = $result_get_rows->fetch())
{
$csv .= '"'.join('","', str_replace('"', '""', $rows_get_rows))."\"\n";
}
echo $csv;
exit;
You should say to PDO, that you want only an associative array or a numbered array:
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_ASSOC))
to get an associative array or
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_NUM))
to get an array indexed by the column number
from PDOStatement::fetch
fetch_style
Controls how the next row will be returned to the caller.
This value must be one of the PDO::FETCH_* constants, defaulting to
value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to
PDO::FETCH_BOTH).
PDO::FETCH_ASSOC: returns an array indexed by column name as returned
in your result set
PDO::FETCH_BOTH (default): returns an array indexed by both column
name and 0-indexed column number as returned in your result set

How to get an array value from the query in Yii 1.1

$us= Yii::app()->db->createCommand()
->select('default_number_of_devices')
->from('user')
->where('id=1')
->queryRow();
echo "$us";
I should have got one value instead of array type because id is unique. But $us appears to be an array instead of single number.
You should use queryScalar() if you want to get single value from single column:
$us = Yii::app()->db->createCommand()
->select('default_number_of_devices')
->from('user')
->where('id=1')
->queryScalar();
echo $us;
queryRow() returns first row from query. And since row usually contains multiple columns, array is expected format (each element of array contains value of single column).

PDO Arrays are not structured as expected [duplicate]

I have a script that is outputting to a CSV file. However, even though there is currently one row in the database, the output I'm getting is echoing out each column from each row in the table twice.
For example:
1,1,John,John,Smith,Smith,2014,2014
Should be
1,John,Smith,2014
This worked fine before I went with PDO and prepared statements, so I'm thinking maybe I'm not understanding how fetch() works correctly.
Below is my code. Any idea what I could be doing wrong?
// get rows
$query_get_rows = "SELECT * FROM Contacts ORDER BY date_added DESC";
$result_get_rows = $conn->prepare($query_get_rows);
$result_get_rows->execute();
$num_get_rows = $result_get_rows->rowCount();
while ($rows_get_rows = $result_get_rows->fetch())
{
$csv .= '"'.join('","', str_replace('"', '""', $rows_get_rows))."\"\n";
}
echo $csv;
exit;
You should say to PDO, that you want only an associative array or a numbered array:
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_ASSOC))
to get an associative array or
while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_NUM))
to get an array indexed by the column number
from PDOStatement::fetch
fetch_style
Controls how the next row will be returned to the caller.
This value must be one of the PDO::FETCH_* constants, defaulting to
value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to
PDO::FETCH_BOTH).
PDO::FETCH_ASSOC: returns an array indexed by column name as returned
in your result set
PDO::FETCH_BOTH (default): returns an array indexed by both column
name and 0-indexed column number as returned in your result set

get all values from SQL column using PHP (only returns one value)

I am querying the database for all values in one column, then putting it into an associative array. The db query is working, but PHP prints an array with only one item.
<?php
$acc_names = mysqli_query($con,"SELECT acc_name FROM accounts"); #successfully returns all values in column in mysql
$acc_names = (mysqli_fetch_assoc($acc_names));
print_r($acc_names); #only one item in array
print_r($acc_names['acc_name']); #that one item
?>
You should use mysqli_fetch_all($acc_names, MYSQLI_ASSOC) instead of mysqli_fetch_assoc.

mysql query to retrieve every single row of data

$q = "SELECT * FROM user";
$res = mysqli_query($conn, $q) or die(mysql_error());
$userList = "";
while($user = mysqli_fetch_array($res))
{
$userList .= $user['userList'].";;";
}
echo $userList;
I don't understand the while part:
Why assign the mysqli_fetch_array to $user using while?
How can the $user have index of userList?
Why concatenate with ;;?
To answer your questions:
i) mysqli_fetch_array() has two possible return values. It either returns an array of the current row that the database result set pointer points to, then advances the pointer to the next row, or it returns false if you have reached the end of the result set. The while() evaluates the value that is set to $row either continuing the loop if it is an array or stopping the loop if $row equals false
ii) The $user array has both numerical indexes for each field (i.e. 0,1,2,... [#fields - 1]) and associative indexes of the column names for the table (i.e. 'field1', 'field2', etc.). In this case one of the fields in the database is userList, so accessing $user['userList'] returns that column value for the row being worked with. BNote that the query itself would have beeter been written as SELECT userList FROM user since that is the only field you are interested in. There is no reason whatsoever to select and transfer all field data if you have no need for it. It is also rarely useful to use just mysqli_fetch_array(), as you rarely need both numerical and associative indexes on the record.It is usually best to specifically request ther associative or numerical array result depending on which you need.
iii) This code is simply building a string rather than an array of results which might be more common. For whatever reason the code writer decided values in the string should be separated by ;;.

Categories