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_exec -- Prepare and execute a SQL statement


resource odbc_exec ( resource connection_id, string query_string)

Returns FALSE on error. Returns an ODBC result identifier if the SQL command was executed successfully.

odbc_exec() will send an SQL statement to the database server specified by connection_id. This parameter must be a valid identifier returned by odbc_connect() or odbc_pconnect().

See also: odbc_prepare() and odbc_execute() for multiple execution of SQL statements.

add a note add a note User Contributed Notes
rob at vendorpromotions dot com
17-Jun-2003 03:29
This opens select statements 'for update' by default in db2.  If you're using db2, you have to tack on 'for read only' at the end to select from SYSCAT.TABLES, for example, without firing an error like

Warning: SQL error: [IBM][CLI Driver][DB2/LINUX] SQL0151N The column "MAXFREESPACESEARCH" cannot be updated. SQLSTATE=42808 , SQL state 42808 in SQLExecDirect

For example :

$query = odbc_exec($conn, "select * from syscat.tables for read only");

will work (only for db2).  I don't know about other databases.

The select statement will work in the 'db2' command line, but not in php, because of this side effect.
rupix at rediffmail dot com
05-Apr-2003 10:05
I tried the following line of code

=odbc_connect("pbk""root","") or die(odbc_errormsg());
$q="insert into pbk values(\"$name\"\"$phone\")";
odbc_exec($odbc$q) or die("<p>".odbc_errormsg());

it does not work. However if I use single quotes instead of \" the thing runs smoothly

thus the following would work

=odbc_connect("pbk""yourworstnightmare","abracadabra") or die(odbc_errormsg());
$q="insert into pbk values('$name', '$phone')";
odbc_exec($odbc$q) or die("<p>".odbc_errormsg());

Also having a user dsn is no good on win2k. Always have a System DSN. I don't know yet what are the implications of the same.
das_yrch at hotmail dot com
07-Mar-2003 07:17
I tried this way to see the results of a query and it works!!

$Conn = odbc_connect
("bbdd_usuaris","","",SQL_CUR_USE_ODBC );

$result=odbc_exec($Conn,"select nom from usuaris;");

        echo "Result is ".odbc_result($result,$i);
miguel dot erill at doymer dot com
24-Jul-2002 05:33
In a previous contribution it was told that if you're running NT/IIS with PHP 3.0.11 you can use MS Access dbs "stored procedures".

That was right, but if those stores procedures have parameters you have to supply them in the command line like this:

$conn_id = odbc_connect( "odbc_test_db", "","", SQL_CUR_USE_DRIVER );
$qry_id = odbc_do( $conn_id, "{CALL MyQuery(".$param.")}" );
martin at NOSPAMkouba dot at
06-Feb-2002 12:37
"[Microsoft][ODBC Microsoft Access Driver] Too few
parameters. Expected 1."

this not so clear to understand error comes when using access-odbc and a field name can't be found. check for correct spelling of fields.
lee200082 at hotmail dot com
22-Jan-2002 09:07
As an addition to the note about square brackets earlier:

Enclosing sql field names in '[' and ']' also allows you to use MS Access reserved words like 'date' and 'field' and 'time' in your SQL query... it seems that the square brackets simply tell Access to ignore any other meaning whatever is inside them has and take them simply as field names.
mh at maxx-web dot de
07-Jan-2002 01:25
Ich habe beim Nutzen von odbc_exec($conn, $sql); einen Bug entdeckt.

Wenn das SQL Statement ein reines "update" Statement ist so wird es nicht in die Datenbank geschrieben.

Erst nach dem Aufruf eines erneuten "Select" Statements, werden die Daten des vorigen Update Statements geschrieben.
$sql="select * from UserQuotas";
odbc_exec($conn, $sql);

Ich weiss nicht ob mein System daran Schuld ist oder ob es wirklich ein Fehler ist. Ein Oracle spezifisches "commit" habe ich zumindest nirgends gefunden.
sk2xml at gmx dot net
21-Nov-2001 10:15
Problem: Fieldnames in SQL-Statement have blanks and [] don't work!

Solution: Try "" instead


SELECT table2.first, table1.[last name] FROM tabel1, table2 -> don't work

SELECT table2.first, table1.\"last name\" FROM tabel1, table2 -> Try this

PS: Don't forget the espace characters !!!
david dot geere at nospam dot talk21 dot com
18-Aug-2001 03:08
Problem: Kept getting FATAL: emalloc() errors when using basic select statements via odbc for informix ....
Solution: Cast the columns to an informix odbc friendly type...I chose varchar(255)
example: select description::varchar(255) from objects;

Don't go through the 4 hours I just went through for :-) me :-(
cfewer1 at home dot com
15-Aug-2001 04:25
If you're receiving a 'Syntax error in INSERT INTO ..<snip>.. SQL State 37000 in SQLExecDirect' error, try enclosing the field names between square brackets.


INSERT INTO whatever ([blah],[who],[what]) VALUES ('blah','blah','blah');

I spent 4 hours tryin to get this insert statement (without the []'s) to work. This seems to have fixed it.

[]'s, apparently according to MS, should be used with table/field names with spaces. Im not sure if this is an MS ODBC thing, or a PHP flaw.

tested with: win32/php4.0.6/apache1.3.20/odbc/mdac2.6sp1
akchu at at ualberta dot ca
08-Jan-2001 12:41
ODBC/MS Access Date Fields:

Matching dates in SELECT statements for MS Access requires the following format:
#Y-m-d H:i:s#

for example:

SELECT * FROM TableName WHERE Birthdate = #2001-01-07 00:00:00#


SELECT * FROM TableName WHERE Birthdate BETWEEN #2000-01-07 00:00:00# AND #2001-01-07 00:00:00#

This took me forever to figure out.
vpil at retico dot com
06-Nov-2000 10:24
Additional links to ODBC_exec:
How to actually write the SQL commands:
Demystifying SQL
Introduction to Structured Query Language
Covers read, add, modify & delete of data.
phobo at at at paradise dot net dot nz
02-Nov-2000 10:26
If Openlink -> MS Access Database fails and gives "Driver Not Capable" error or "No tuples available" warning, use the SQL_CUR_USE_ODBC cursor when using odbc_connect()...

andreas dot brunner at rubner dot com
08-Jul-2000 06:54
I wanted to access an MSAccess database via ODBC. The connection functioned without problems, but when I placed a SQL statement into my odbc_exec() i always got an error:
Warning: SQL error: [Microsoft][ODBC Driver Manager] Driver does not support that function, SQL state IM001 in SQLSetStmtOption in \\Server\directory/test.php3 on line 19.

Resolved my problem by myself: i simply had to install a new odbc-driver from the microsoft homepage.
gross at arkana dot de
28-Oct-1999 08:03
If you're running NT/IIS with PHP 3.0.11 and want to use MS Access dbs with "stored procedures" you can send an ODBC SQL query like:

$conn_id = odbc_connect( "odbc_test_db", "", "", SQL_CUR_USE_DRIVER );
$qry_id = odbc_do( $conn_id, "{CALL MyQuery}" );

This way you don't need to integrate query strings like

SELECT * FROM TblObject WHERE (((TblObject.something) Like "blahblahblah"));

in the php file. You directly call the query "MyQuery" that was generated by MS Access.
rmkim at uwaterloo dot ca
26-Aug-1999 01:13
for Win32(NT) and MSAcess 2000, whenever you retrieve a date column/field, php will automatically convert it to 'yyyy/mm/dd hh:mm:ss' format regardless of the style of date you've denoted in Access.
This seems to pose a problem when you exec SELECT, UPDATE, or DELETE queries, but strangley INSERT works fine. I've tried parsing the date into the desired format, but php still yells criteria mismatch.

 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