Table of Contents
odbc连接数据库,odbc数据库
Home php教程 php手册 odbc连接数据库,odbc数据库

odbc连接数据库,odbc数据库

Jun 13, 2016 am 08:40 AM
Connect to the database

odbc连接数据库,odbc数据库

  php操作数据库有很多种方式,如mysql,mysqli,odbc,pdo等。MySQL 是 PHP 操作 MySQL 数据库最原始的 Extension。MySQLi 的 i 代表 Improvement ,提供了相对进阶的功能,就 Extension 而言,本身也增加了安全性,这都只是操作特定种类的数据库的,当你更换其他类型的数据库时又得使用其他类型数据库的操作方法来操作数据库,也就是得重写代码,这是很麻烦的。有没有一种方法可以是通用型的,使得可以一次编写多次使用,兼容各种数据库呢?答案当然是肯定的,那就是obbc以及pdo了。pdo是php 5新出的用来操作各种数据库的扩展,是专属于php的,就类似于Java的jdbc。这个后面再讲。现在先讲一讲odbc。

  什么是ODBC?

     ODBC 是一个软件驱动程序系统,用于连接编程语言与数据存储。ODBC 是一个免费的开放源码系统,出现于 1992 年,它试图通过编程语言和数据库查询访问(SQL 标准化)来标准化连接方法,比如功能和配置。

  ODBC 的作用是充当接口或连接器,它具有双重设计目标:首先,对于 ODBC 系统,它充当的是编程语言系统,其次,对于数据存储系统,它充当的是 ODBC 系统。所以,ODBC 需要一个 “对 ODBC 而言是编程语言” 的驱动程序(例如 PHP-ODBC 库)和一个 “对数据存储系统而言是 ODBC” 的驱动程序(比如 MySQL-ODBC 库)。除了 ODBC 系统本身之外,ODBC 还可以处理数据源的配置,允许数据源和编程语言之间存在模糊性。

     怎么使用odbc?

     当使用odbc时, PHP 开发变得 “与数据库连接器无关”。它对数据库(比如 MySQL、PostgreSQL、SQLite、Microsoft SQL Server®、IBM® DB2®、Sybase、OpenLink Virtuoso、FileMaker 和 Microsoft Office® Access®)使用像 odbc_query() 这样的函数。还可以将 ODBC 用于 CSV 和 Excel 电子表格,具体取决于正确的 ODBC 驱动程序设置。下面看看怎么使用:

      1.首先odbc扩展并开启,通过phpinfo()查看到该模块并且状态为enabled;

  2.连接到 ODBC

1

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

Copy after login

1

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

Copy after login

  3.取回记录

1

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

Copy after login

1

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

Copy after login

1

odbc_fetch_row($rs)

Copy after login

  4.从记录中取回字段

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.关闭 ODBC 连接

1

2

3

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

 

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

Copy after login

注:其他操作函数:http://php.net/manual/zh/ref.uodbc.php

  

  ODBC 实例

  下面的实例展示了如何首先创建一个数据库连接,接着创建一个结果集,然后在 HTML 表格中显示数据。

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

 

 
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
3 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