Get the App
SLTechnology News&Howtos  ›  Database  › 

How to create ODBC in php

Shulou Source: shulou.com Published: 2022-05-31 13:49:34 11月03日 Update

This article introduces the relevant knowledge of "how to create ODBC in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ODBC is an application programming interface (Application Programming Interface,API) that gives us the ability to connect to a data source, such as an MS Access database.

Create an ODBC connection

With an ODBC connection, you can connect to any database on any computer in your network, as long as the ODBC connection is available.

This is the way to create an ODBC connection to the MS Access database:

Open the administrative tools icon in the control panel.

Double-click the data source (ODBC) icon in it.

Select the system DSN tab.

Click add in the system DSN tab.

Select Microsoft Access Driver. Click finish.

In the next interface, click Select to locate the database.

Give the database a data source name (DSN).

Click OK.

Please note that this configuration must be done on the same computer as your website. If Internet Information Services (IIS) is running on your computer, the above instructions will take effect, but if your website is located on a remote server, you must have physical access to that server, or ask your host provider to establish a DSN for you.

Connect to ODBC

The odbc_connect () function is used to connect to an ODBC data source. The function takes four parameters: the data source name, user name, password, and optional pointer type.

The odbc_exec () function is used to execute the SQL statement.

Example

The following example creates a connection to the DSN named northwind without a user name and password. Then create and execute a SQL statement:

$conn=odbc_connect ('northwind','',''); $sql= "SELECT * FROM customers"; $rs=odbc_exec ($conn,$sql); retrieve record

The odbc_fetch_row () function is used to return records from the result set. If the row can be returned, the function returns true, otherwise it returns false.

The function takes two parameters: the ODBC result identifier and the optional line number:

Odbc_fetch_row ($rs) retrieves fields from the record

The odbc_result () function is used to read fields from a record. The function takes two parameters: the ODBC result identifier and the field number or name.

The following line of code returns the value of the first field from the record:

$compname=odbc_result ($rs,1)

The following line of code returns the value of the field named "CompanyName":

$compname=odbc_result ($rs, "CompanyName"); close the ODBC connection

The odbc_close () function closes the ODBC connection.

Odbc_close ($conn); that's all for "how to create ODBC in php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data functions fields databases data sources facets parameters results websites services selections two code content icons instances passwords more servers identifiers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Linux MariaDB Shulou Information