Table of Contents
odbc connects to the database, odbc database
Home Backend Development PHP Tutorial odbc connection database, odbc database_PHP tutorial

odbc connection database, odbc database_PHP tutorial

Jul 12, 2016 am 08:53 AM
Connect to the database

odbc connects to the database, odbc database

There are many ways for PHP to operate the database, such as mysql, mysqli, odbc, pdo, etc. MySQL is the original Extension for PHP to operate the MySQL database. The i in MySQLi stands for Improvement, which provides relatively advanced functions. As far as Extension is concerned, it also increases security. These are only for operating specific types of databases. When you change other types of databases, you have to use other types of databases. To operate the database, you have to rewrite the code, which is very troublesome. Is there a method that can be universal, so that it can be written once and used multiple times, and is compatible with various databases? The answer is of course yes, that is obbc and pdo. pdo is a new extension of PHP 5 for operating various databases. It is exclusive to PHP and is similar to Java's jdbc. More on this later. Now let’s talk about odbc.

 What is ODBC?

ODBC is a software driver system used to connect programming languages ​​and data storage. ODBC is a free and open source system that emerged in 1992 and attempts to standardize connection methods such as functionality and configuration through programming languages ​​and database query access (SQL Standardization).

 ODBC functions as an interface or connector, and it has dual design goals: first, for the ODBC system, it acts as a programming language system, and secondly, for the data storage system, it acts as an ODBC system. Therefore, ODBC requires a "programming language to ODBC" driver (such as the PHP-ODBC library) and a "ODBC to data storage system" driver (such as the MySQL-ODBC library). In addition to the ODBC system itself, ODBC handles the configuration of data sources, allowing ambiguity between data sources and programming languages.

How to use odbc?

When using odbc, PHP development becomes “database connector agnostic”. It uses functions like odbc_query() for databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server®, IBM® DB2®, Sybase, OpenLink Virtuoso, FileMaker, and Microsoft Office® Access®. You can also use ODBC with CSV and Excel spreadsheets, depending on the correct ODBC driver settings. Let’s see how to use it:

1. First, odbc is extended and enabled, and the module is viewed through phpinfo() and the status is enabled;

 2. Connect to ODBC

1

odbc_connect() 函数用于连接到 ODBC 数据源。该函数有四个参数:数据源名、用户名、密码以及可选的指针类型。

Copy after login

1

odbc_exec() 函数用于执行 SQL 语句。

Copy after login

 3. Retrieve records

1

//odbc_fetch_row() 函数用于从结果集中返回记录。如果能够返回行,则函数返回 true,否则返回 false。

Copy after login

1

//该函数有两个参数:ODBC 结果标识符和可选的行号:

Copy after login

1

odbc_fetch_row($rs)

Copy after login

 4. Retrieve fields from the record

1

2

3

4

5

6

7

odbc_result() 函数用于从记录中读取字段。该函数有两个参数:ODBC 结果标识符和字段编号或名称。

 

下面的代码行从记录中返回第一个字段的值:

 

   $compname=odbc_result($rs,1<span>);<br />

下面的代码行返回名为 "CompanyName" 的字段的值:<br />

    $compname=odbc_result($rs,"CompanyName");<br /></span>

Copy after login

5. Close the ODBC connection

1

2

3

odbc_close() 函数用于关闭 ODBC 连接。

 

  odbc_close($conn);<br /><br />

Copy after login

Note: Other operating functions: http://php.net/manual/zh/ref.uodbc.php

 

 ODBC instance

The following example shows how to first create a database connection, then create a result set, and then display the data in an HTML table.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<html>

<body>

 

<?<span>php

</span><span>$conn</span>=<span>odbc_connect</span>('northwind','',''<span>);

</span><span>if</span> (!<span>$conn</span><span>){   <br /></span><span>   exit</span>("Connection Failed: " . <span>$conn</span><span>);}

</span><span>   $sql</span>="SELECT * FROM customers"<span>;

</span><span>   $rs</span>=<span>odbc_exec</span>(<span>$conn</span>,<span>$sql</span><span>);

  </span><span>if</span> (!<span>$rs</span><span>){ <br />      </span><span>exit</span>("Error in SQL"<span>);}

     </span><span>echo</span> "<table><tr>"<span>;

     </span><span>echo</span> "<th>Companyname</th>"<span>;

     </span><span>echo</span> "<th>Contactname</th></tr>"<span>;

     </span><span>while</span> (<span>odbc_fetch_row</span>(<span>$rs</span><span>)){

      </span><span>$compname</span>=<span>odbc_result</span>(<span>$rs</span>,"CompanyName"<span>);

      </span><span>$conname</span>=<span>odbc_result</span>(<span>$rs</span>,"ContactName"<span>);<br />      </span><span>echo</span> "<tr><td><span>$compname</span></td>"<span>;

      </span><span>echo</span> "<td><span>$conname</span></td></tr>"<span>;

  }

  </span><span>odbc_close</span>(<span>$conn</span><span>);

  </span><span>echo</span> "</table>"<span>;

</span>?>

 

</body>

</html>

Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125064.htmlTechArticleodbc connects to the database, odbc database PHP has many ways to operate the database, such as mysql, mysqli, odbc, pdo, etc. MySQL is the original Extension for PHP to operate the MySQL database. MySQLi's...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read the first few records in a database using PHP? How to read the first few records in a database using PHP? Mar 22, 2024 am 10:03 AM

How to read the first few records in a database using PHP? When developing web applications, we often need to read data from the database and display it to the user. Sometimes, we only need to display the first few records in the database, not the entire content. This article will teach you how to use PHP to read the first few records in the database and provide specific code examples. First, assume that you have connected to the database and selected the table you want to operate on. The following is a simple database connection example:

How to deal with SQLException when connecting to database in Java? How to deal with SQLException when connecting to database in Java? Jun 24, 2023 pm 09:23 PM

In Java programs, connecting to the database is a very common operation. Although ready-made class libraries and tools can be used to connect to the database, various abnormal situations may still occur during program development, among which SQLException is one of them. SQLException is an exception class provided by Java. It describes errors that occur when accessing the database, such as query statement errors, table non-existence, connection disconnection, etc. For Java programmers, especially those using JDBC (Java Data

How to connect to the database in go language How to connect to the database in go language Dec 12, 2023 pm 03:51 PM

Go language connects to the database by importing the database driver, establishing a database connection, executing SQL statements, using prepared statements and transaction processing. Detailed introduction: 1. Import the database driver and use the github.com/go-sql-driver/mysql package to connect to the MySQL database; 2. Establish a database connection and provide the database connection information, including the database address, user name, password, etc. Establish a database connection and so on through the sql.Open function.

Using Go language to connect to the database: improve application performance and efficiency Using Go language to connect to the database: improve application performance and efficiency Jan 23, 2024 am 08:57 AM

Using Go language to connect to the database: Improving application performance and efficiency As applications develop and the number of users increases, data storage and processing become more and more important. In order to improve the performance and efficiency of applications, properly connecting and operating the database is a crucial part. As a fast, reliable, and highly concurrency development language, Go language has the potential to provide efficient performance when processing databases. This article will introduce how to use Go language to connect to the database and provide some code examples. Install database driver using Go language

Steps and techniques for implementing product inventory counting with PHP Steps and techniques for implementing product inventory counting with PHP Aug 18, 2023 am 08:39 AM

Steps and techniques for implementing product inventory in PHP In the e-commerce industry, product inventory management is a very important task. Timely and accurate inventory counting can avoid sales delays, customer complaints and other problems caused by inventory errors. This article will introduce the steps and techniques on how to use PHP to implement product inventory counting, and provide code examples. Step 1: Create a database First, we need to create a database to store product information. Create a database named "inventory" and then create a database named "prod

Getting Started with Go Language: Basic Concepts of Database Connections Getting Started with Go Language: Basic Concepts of Database Connections Jan 23, 2024 am 08:17 AM

Learn Go language: basic knowledge of connecting to databases, specific code examples are required. Go language is an open source programming language. Its simple and efficient features make it loved and used by more and more developers. During the development process, it is often necessary to establish a connection with the database to perform operations such as reading, writing, updating, and deleting data. Therefore, learning how to connect to a database in Go language is a very important skill. Database driver In the Go language, a database driver is required to connect to the database. At present, the main database drivers of Go language are:

How to install and configure PHP on Ubuntu system to connect to MSSQL database How to install and configure PHP on Ubuntu system to connect to MSSQL database Feb 29, 2024 am 10:06 AM

Installing and configuring PHP on Ubuntu systems to connect to MSSQL databases is a common task, especially when developing web applications. In this article, we will introduce how to install PHP, MSSQL extensions and configure database connections on Ubuntu systems, while providing specific code examples. Step 1: Install PHP and MSSQL extensions Install PHP First, you need to make sure that PHP is installed on your Ubuntu system. PHP can be installed with the following command: sudoaptu

What are the important functions of MySQL's Jar package? What are the important functions of MySQL's Jar package? Mar 01, 2024 pm 09:45 PM

Title: What are the important functions of MySQL’s Jar package? MySQL is a popular relational database management system that many Java developers use when developing applications. In order to interact with the MySQL database in a Java project, the official Java driver Jar package provided by MySQL is usually used. MySQL's Jar package has many important functions. This article will introduce some of them and provide specific code examples. 1. Connect to MyS

See all articles