Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : ODBC Functions (Unified) : odbc_result

odbc_result

Get result data (PHP 4, PHP 5)
mixed odbc_result ( resource result_id, mixed field )

Examples ( Source code ) » odbc_result

<?php
$item_3   
odbc_result($Query_ID3);
$item_val odbc_result($Query_ID"val");
?>

Code Examples / Notes » odbc_result

huevo dot sp

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++) {
 if(odbc_result($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);
 if($temp){
   ...
   echo $temp;
   ...
 }
}


murat

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.
e.g.
echo "The date is "
.date("r",strtotime(odbc_result($myquery,"mydate")));


nomail

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.


spooky

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


fate

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!


dave

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.


baoshenyi

I use one store procedure to retrieve value of identifier, other, name, section,data and datecreated coulmns to variable from SQL server table
using $odbc_result = odbc_exec($connect,$query); function.
After that, I using following code,
for($f=1;$f<=odbc_num_fields($odbc_result);$f++) {echo "<td style=\"font-weight:bold\">$f ".odbc_field_name($odbc_result,$f)."</td>";}
echo "</tr></table>";
odbc_fetch_row($odbc_result);
echo odbc_result($odbc_result,1)."
";
echo odbc_result($odbc_result,2)."
";
echo odbc_result($odbc_result,3)."
";
echo odbc_result($odbc_result,4)."
";
echo odbc_result($odbc_result,5)."
";
echo odbc_result($odbc_result,6)."
";
The result is as following,
1 identifier 2 other 3 name 4 section 5 data 6 datecreated
id1
other2
name3
section4
Warning: odbc_result() [function.odbc-result]: SQL error: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index, SQL state S1002 in SQLGetData in d:\lawdepot_test\contracts\common\LicensingSQL.php on line 630
2005-03-16 18:12:00
I can not get "data"(Text column) back. Firstly, I think "data" column is too long for odbc_result($odbc_result,5) function, but after I check my old colde, I found I can get the "data" back using same function odbc_result().
I would like to hear any suggestions from you. Depressed on this question.
Michael


vlad dot posea

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!


user

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.


dinin

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
-D


dac

FWIW, ADO and similar ODBC-using interfaces also cannot distinguish between A.id and B.id. The easy solution is aliasing in SQL:
Consider a table "A" consisting only of the column "id". Next, consider the following query:
SELECT * FROM A JOIN B ON A.id=B.id
With ODBC, you'd be forced to use the numerical index, rather than the name. However, you could rewrite the query, too:
SELECT A.id AS A_id, B.id AS B_id FROM A JOIN B ON A.id=B.id
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.


22-jan-2003 02:43

From http://www.php.net/manual/en/function.odbc-longreadlen.php but relevant here, also.
An alternative is to adjust your php.ini file and set:
odbc.defaultlrl=65536
Or something else sufficiently large.
lrl = long read length
to get around the limit on returned chars.


09-mar-2001 08: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ça está no listar. Se você usar o odbc_result_all, ele mostrará inclusive os nomes das colunas. O que normalmente não é interessante.
Porem, se o resultado é algum calculo, o odbc_result funciona perfeitamente, por exemplo:
Se o resultado em um result_all for:
@valor
1500.00
você pode usar odbc_result($procedure,1)
Isto retornará apenas o resultado do cálculo.


jniels23

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 :
mysql_result($res,$count,"Role.Name");  
mysql_result($res,$count,"Organisation.Name");
but with odbc you do :
odbc_result($res,$count,"Name");
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.


aka mbg

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").


zhiliin

$cc=odbc_result($result_id,'val');
if(isset($cc){
echo "val=\"\"";//exist
}else{
echo "val=null";
}
try this, Best Regard.


Change Language


Follow Navioo On Twitter
odbc_autocommit
odbc_binmode
odbc_close_all
odbc_close
odbc_columnprivileges
odbc_columns
odbc_commit
odbc_connect
odbc_cursor
odbc_data_source
odbc_do
odbc_error
odbc_errormsg
odbc_exec
odbc_execute
odbc_fetch_array
odbc_fetch_into
odbc_fetch_object
odbc_fetch_row
odbc_field_len
odbc_field_name
odbc_field_num
odbc_field_precision
odbc_field_scale
odbc_field_type
odbc_foreignkeys
odbc_free_result
odbc_gettypeinfo
odbc_longreadlen
odbc_next_result
odbc_num_fields
odbc_num_rows
odbc_pconnect
odbc_prepare
odbc_primarykeys
odbc_procedurecolumns
odbc_procedures
odbc_result_all
odbc_result
odbc_rollback
odbc_setoption
odbc_specialcolumns
odbc_statistics
odbc_tableprivileges
odbc_tables
eXTReMe Tracker