downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003


(PHP 3>= 3.0.6, PHP 4 )

odbc_result -- Get result data


string odbc_result ( resource result_id, mixed field)

Returns the contents of the field.

field can either be an integer containing the column number of the field you want; or it can be a string containing the name of the field. For example:

$item_3 = odbc_result ($Query_ID, 3);
$item_val = odbc_result ($Query_ID, "val");

The first call to odbc_result() returns the value of the third field in the current record of the query result. The second function call to odbc_result() returns the value of the field whose field name is "val" in the current record of the query result. An error occurs if a column number parameter for a field is less than one or exceeds the number of columns (or fields) in the current record. Similarly, an error occurs if a field with a name that is not one of the fieldnames of the table(s) that is(are) being queried.

Field indices start from 1. Regarding the way binary or long column data is returned refer to odbc_binmode() and odbc_longreadlen().

add a note add a note User Contributed Notes
vlad dot posea at mymail dot ro
02-May-2003 06:22
i use odbc and mysql and i noticed after losing a lot of time that if you write something like this:
echo odbc_result($result,1);
echo odbc_result($result,1);
the second echo will fail. so it's more useful to save the result of odbc_result in a variable and use it later
like that: $var=odbc_result($result,1);
i hope this will be useful!
15-Mar-2003 01:27
It took me a while to find out why there was a problem with retrieving data with odbc_result statement.

Warning: SQL error: [Microsoft][ODBC SQL Server Driver]
SQL state S1002 in SQLGetData

State S1002 means that invalid column index has been used.
I checked it hundred times - column index was OK.

Finally i ordered calls of odbc_result statement according to order of fields in the table. It helped.

Best regards
28-Feb-2003 06:29
About memo fields accessing through ODBC.
I got error:

SQL error: [Microsoft][ODBC Visual FoxPro Driver]Invalid cursor position, SQL state S1109 in SQLGetData SQL error...

In fact there is no necessity to change Memo to Text type of field in database. Example of source code:

class ds_fconstr extends db_base_table
  var $db_fields  = array('name' => "",
                         'password' => "");

  $query = "SELECT * FROM ds_fconstr";
  $odbc  = odbc_connect (DB_DSN, "", "")
  $req = odbc_exec ($odbc, $query)

  // create this class
  $temp = new ds_fconstr();

  // error inside of this function (of parent class)
  // when try call "odbc_result" for "memo" field
  $temp->setFieldsValue ($req);

   // all OK here
  foreach ($temp->db_fields as $name => $value)
    $temp->db_fields[$name] = odbc_result($req,$name);

// where base class is following
Class db_base_table
  var $db_fields = array();
  // set value of fields from fetched row
  // ----------------------------
  function setFieldsValue($req)
      foreach ($this->db_fields as $name => $value)
          // error is HERE (only for "memo" fields, others are ok),
        // because (possible) PHP do not transfer
        // correctly result of odbc_exec with "memo"
        $this->db_fields[$name] = odbc_result($req,$name);

So, when i call directly "odbc_result" in the same function where i have called "odbc_exec" then all is right (see line "// all OK here").
22-Jan-2003 10:43
From but relevant here, also.

An alternative is to adjust your php.ini file and set:
Or something else sufficiently large.
lrl = long read length

to get around the limit on returned chars.
fate at doityourself dot com
21-Jan-2003 01:14
if you want to quickly change a website from odbc- to mysql-data-access, you could use a simple function like this one:
function myresult ($cur,$nr) {
   return mysql_result($cur,0,mysql_field_name($cur,$nr-1));
and just do a global replace from "odbc_result" to "myresult".

beware, you should only use this if you don't care too much about performance, as this will start a query for every field you request - the better way is of course using mysql_fetch_row!
murat at nospam dot robcol dot k12 dot tr
15-Jul-2002 04:44
When trying to get a date/time field from an Access database, odbc_result returns the date as text (e.g. 1998-07-11 21:12:23). You may use strtotime function to convert this into a format which can be used with PHP.

echo "The date is "
huevo dot SP at M dot earthling dot net
01-Aug-2001 05:46
With an Access 2K database, odbc_result will only work the first time it is called on a memo field (odbc_field_type returns 'LONGCHAR').

$good = odbc_result($result,'Description');
# $good holds the field value
$fail = odbc_result($result,'Description');
# $fail holds false

This tripped me up when I did something like this:

for ($i=1; $i<=odbc_num_fields($result); $i++) {
    ... more code ...
    echo odbc_result($result,$i);
    ... snip ...

For any field type except memo, this code works perfectly.  But on memo fields, odbc_result returns false on the second call and nothing is echoed.

Be careful, use a temporary variable instead of multiple calls to odbc_result.  (It's good programming practice anyway)

for (...) {
  $temp = odbc_result($result,$i);
    echo $temp;
user at frosch dot org
30-May-2001 10:55
Hopefully useful note on accessing TEXT fields on Windows, using PHP's ODBC support to access Sybase*. With the following code:

"SELECT status AS projstatus,oid AS projident,LOWER(title) AS projtitel,startsOn AS projanfang,terminatesOn AS projende,description AS projinfo FROM Project ORDER BY projtitel ASC"

I was getting a strange error:

  Warning: SQL error: [INTERSOLV][ODBC SQL Server driver][SQL Server]Invalid column name 'projanfang'. , SQL state S0022 in SQLGetData in [**scriptname and path removed**] on line 126

even though I was clearly selecting a field as 'projanfang'. The reason I worked out eventually is that Sybase/ODBC attempts to do a conversion on the TEXT field 'description', which fails since the limit for CONVERT is 255 characters, and TEXT is a field type with a  2 GB limit. I am not sure why it doesn't work implicitly, but it does explicitly. The following code will work without errors:

  "SELECT status AS projstatus,oid AS projident,LOWER(title) AS projtitel,startsOn AS projanfang,terminatesOn AS projende,CONVERT(CHAR(255),description) AS projinfo FROM Project ORDER BY projtitel ASC"

What you do if you have a need for more than 255 characters of your text field, I do not know :(. I have also tried longreadlen, but I couldn't work out how it could be used.

* System details: Windows NT 4.0 SP6a, IIS 4.0, Sybase 11.5 Adaptive Enteprise, PHP 4.0.5.
10-Mar-2001 04:29
Estou adicionando esta mensagem porque demorei a conseguir executar um determinado procedimento.
Se voc?possui procedures armazenadas em algum banco, como Sybase Anywhere ou MSSql, voc?pode se referenciar as mesmas da seguinte forma:
$procedure = odbc_exec($conexao,"execute pr_calc_saldo_aplic $cd_coop, $conta, $cd_produto, $titulo, $in_listar, $vl_saldo_aplic_decimal");
A diferen蓷 est?no listar. Se voc?usar o odbc_result_all, ele mostrar?inclusive os nomes das colunas. O que normalmente n緌 ?interessante.
Porem, se o resultado ?algum calculo, o odbc_result funciona perfeitamente, por exemplo:
Se o resultado em um result_all for:
voc?pode usar odbc_result($procedure,1)
Isto retornar?apenas o resultado do c嫮culo.
nomail at nomail dot nomail
04-Dec-2000 09:00
Problem: Function returns a max of 4095 bytes of a cell with one call.

Tip: If you have a cell containing more than 4095 bytes, write a loop and call the cell over and over as long return is not "". All returns can be added up to the whole string.
dac at felspar dot com
28-Oct-2000 10:43
FWIW, ADO and similar ODBC-using interfaces also cannot distinguish between and The easy solution is aliasing in SQL:

Consider a table "A" consisting only of the column "id". Next, consider the following query:


With ODBC, you'd be forced to use the numerical index, rather than the name. However, you could rewrite the query, too:


This can be better on three counts:

Firstly, unless you really want everything in the result set, it might be faster. SQL servers can be faster with "*", but often the networking will benefit from less data (Or a smaller tuple width, if you're into database jargon).

Secondly, since you're forced into thinking about what you want out of the query, you'll probably write better SQL as a result.

Thirdly, if you change the query - or tables - slightly, you don't have to revisit all your code to cope with the change.

Aplogies for stating the obvious, and having no imagination with my examples.
dinin at fas dot harvard dot edu
22-Jul-2000 11:35
Here's a limitation that isn't mentioned anywhere (that I could see) and gave me a rather large headache for a couple of hours trying to figure out why my database wasn't initializing correctly.
If you are trying to retrieve a large collection of fields from a database, be aware that odbc_result may only return up to 33 result columns.  Any more than that, and it generates a "result out of range" warning in your script.
(I tried adjusting to have 32 fields, with the same bug.) It generates an error "Warning: Field index is larger than the number of fields inyour-script.php on line 70" ANY time you try to retrieve the last field of a sufficiently large record. What worked for me was just to write the last column twice... that way, the query has 34 fields, but the last two are the same. You know it'll crash if you ask for #34, but just use ODBC_result(current_query, 33) and you won't have a problem. Good luck

dave at quiver dot com
15-Jul-2000 02:44
If I use and MS Access 2000 database with fields of type memo, I get:
"Warning: SQL error: [Microsoft][ODBC Driver Manager] Invalid cursor state, SQL state 24000 in SQLGetData in D:\Inetpub\wwwroot\xxx\xxx.php on line XxX." If I change to type "text," everything's cool.
zhiliin at 188 dot net
19-Dec-1999 06:04
echo "val=\"\"";//exist
echo "val=null";
try this, Best Regard.
jniels23 at csc dot com
25-Mar-1999 12:31
Beware if you have fields with the same name in a result

$res = odbc_exec($conn,"select * from PeopleMR,People,Role,Organisation".
" WHERE PeopleMR.MeetingID = $MeetingID" .
" ORDER BY People.Surname");

I have the field "Name" from both TABLE Role and TABLE Organisation, as for MySQL you would do :


 but with odbc you do :


this gives you the result of TABLE Role "Name" so you have to find the Field Number for the Organisation"Name" to have the correct result.

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST