Pages

Subscribe:

Ads 468x60px

Labels

2015年9月3日 星期四

Using ODBC with PHP

資料來源


Using ODBC with PHP ODBC is one of Microsoft's earliest technologies for connecting to databases. It is very popular and widely used. This article describes how to setup and use ODBC from a PHP perspective.
Also see DSN-less connections with PHP.

What is ODBC?

Open DataBase Connectivity is an Application Programming Interface (API) that allows a programmer to abstract a program from a database. When writing code to interact with a database, you have to add code that talks to a particular database using a proprietary API. If you want your program to talk to an Access, FoxPro and Oracle databases you have to code your program with three different database API's. This can be quite the daunting task causing much grief. Now, enter ODBC...
When programming to interact with ODBC you only need to use the ODBC API (a combination of ODBC extension function calls and the SQL language) to talk to different database products. The ODBC Manager will figure out how to contend with the type of database you are targeting. Regardless of the database type you are using, all of your calls will be to the ODBC API. All that you need to do is have installed an ODBC driver that is specific to the type of database you will be using.

Creating A ODBC Data Source Name (DSN)

After you have ensured that the ODBC driver for your database is installed, you normally need to create a Data Source Name (DSN), which contains all the connection details required to login (accept maybe id and password).
Walkthrough: How to Setup a DSN. A pictorial step-by-step guide. The next button is hidden on the bottom-right. Sorry but we Windows programmers are not user interface experts ;-)

Connecting to ODBC

There is an excellent tutorial on using PHP's ODBC extension at ASPToday, a popular ASP web site.
An example taken from the above article:

# query the users table for name and surname
$query = "SELECT name, surname FROM users";
# perform the query $result = odbc_exec($connect, $query);
# fetch the data from the database while(odbc_fetch_row($result)){ $name = odbc_result($result, 1); $surname = odbc_result($result, 2); print("$name $surname\n"); }
# close the connection odbc_close($connect); ?>

沒有留言:

張貼留言