The
odbc_exec()
function nearly corresponds to the
mysql_query()
function.
The only difference is that the
odbc_exec()
function needs two parameters where
the first parameter is the
connection id
returned by the
odbc_connect
statement:
resource odbc_exec ( resource connection_id, string query_string)
To get each row by row in an array equivalent to the
mysql_fetch_row()
function
the
odbc_fetch_into()
function can be used without great modifications. There
are only syntax differences in both statements in our case.
A problem can occur when converting SQL statements with variables to ODBC.
In our MySQL example above, the
$id
variable is in single quote (
`
) whereas in
the DB2 example the single quotes are removed. The background for the change
is that the table column
ID
is of type integer, and ODBC does not convert strings
to integer automatically whereas in the MySQL function it does.
Attention:
When using ODBC functions, variable contents in a SQL
statement are
not
converted to the correspondent type of the database
column. ODBC expects that the variable in the SQL statement has the correct
data type defined.
INSERT, UPDATE,and DELETE statements
In fact for all three SQL commands,
INSERT
,
UPDATE
, and
DELETE
the same
MySQL and ODBC functions are used as discussed for the
SELECT
statement:
mysql_query($sql_statement)
odbc_exec($db,$sql_statement)
In Example 7 26 and Example 7 27, the difference between the MySQL and
ODBC functions for the INSERT statement is shown.
Example 7 26 MySQL insert statement
$ret=mysql_query("INSERT INTO warenkorb (user_id, ktg_id) VALUES
( $user_id , $ktg_id )");
if ($ret){
$text="Item ".$artnr." was inserted in the shopping cart";
}else{
$text="Insert failure of item ".$artnr." into the shopping cart";
}
echo $text;
Example 7 27 DB2 insert statement
$ret=odbc_exec($db,"INSERT INTO warenkorb (user_id, ktg_id) VALUES
( $user_id , $ktg_id )");
if ($ret){
Chapter 7. Application porting
181
footer
Our partners:
PHP: Hypertext Preprocessor Cheap Web Hosting
JSP Web Hosting
Ontario Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Java Hosting
Cheapest Hosting
Visionwebhosting.net Business web hosting division of Vision Web Hosting Inc.. All rights reserved