Table of Contents
ERROR - Could not connect to Oracle
ERROR - Could not parse SQL statement.
Error - no rows returned!
Home php教程 php手册 PHP+Oracle(OCI)初步

PHP+Oracle(OCI)初步

Jun 21, 2016 am 09:09 AM
lt offset oracle php quot

oracle

从Oracle(OCI)开始

在越来越多的PHP用户选择Oracle作为他们的数据库的情况下,如何访问PHP发布环境下的Oracle接口变的越发的重要了。我们将从快速的浏览一个简单基本且是更加明确的Oracle 的情况开始我们的旅程。Oralce 和Oralce8 使用PHP的OCI8 函数库。在PHP手册里有简要的说明(http://www.php.net/manual/ref.oci8.php):

这些函数允许你访问Oracle 和 Oracle7d 的数据库,它们使用的是Oracle8 Call-Interface(OCI8)。你将需要Oracle8 的客户端函数库来使用这些扩展功能。

这些扩展功能比起标准的Oracle扩展功能来更加的灵活,它支持PHP全局及本地变量及与Oracle的连编,有完整的LOB,FILE及ROWID的支持并且允许你定义用户补充变量。

从这里开始,我将使用“Oracle”指代任何Oracle版本,这篇文章假定你已经安装并运行了PHP和Oracle。而Oracle的帮助可以在http://www.oracle.com 或者 http://technet.oracle.com 找到。

本文的观点是
1.连接到ORALCLE
2.建立并运行SQL语句
3.显示结果
4.限制/偏移量 接近“标记页数”的结果

作为一个附加的功能。你可以看看如何完成显示一个从很大的数据结果得出的限制/偏移量的查询。

/*我们将从一个基础的使用显示一个方案。在这个例子里使用的文件是由Rod Kreisler编写的新版本的文章(http://www.phpbuilder.com/columns/rod20000221.php3)。新版本的文件可以在http://php.socket7.net 找到。把$offset设置为0,如果被设置为0或者空的话。
*/
if (empty($offset) || $offset $offset=0;
}

/*我们将从一个基础的使用显示一个方案。在这个例子里使用的文件是由Rod Kreisler编写的新版本的文章(http://www.phpbuilder.com/columns/rod20000221.php3)。新版本的文件可以在http://php.socket7.net 找到。把$offset设置为0,如果被设置为0或者空的话。
*/
if (empty($offset) || $offset $offset=0;
}


$limit = 3;
/*
连接到Oralce,如果你愿意的话,可以在连接数据库的时候直接使用ORACLE_SID。使用PHP作为CGI使用非连续的连接,现在让我们在这个例子里使用这种非连续的连接。在windows里这个可以在注册表里设置,而在unix里你可以使用 putenv()函数putenv(“ORALCE_SID=ORASID”);
或者 ,如果你愿意,你可以在连接数据库的时候直接参考你的ORACLE_SID,让我们在这个例子里直接参考ORACLE_SID。
如果你使用PHP作为APACHE的模块的话,你可以使用持续连接。而使用非持续连接在使用PHP时作为CGI时,我们在这个例子里使用非持续连接。
*/

$conn = OCILogon("user_name", "password", "ORASID");

/*
错误提示。在没有数据库被连接的情况下,将会显示的错误信息并且退出脚本的执行。
*/

if (!$conn) {
echo "

ERROR - Could not connect to Oracle

";
exit;
}

/*
如果连接成功,$conn就是一个连接符。否则,脚本将结束并且输出“Could not connect to Oracle”的错误信息
现在开始来分析并且创建你的SQL语句,由无限制的查询结果得到可以记数的记录。
在这里的格式语句“Select count(*) from table_name”相当于count(),是个SQL函数将会在数据库里执行。比由依靠PHP计算得到的返回结果要好。
*/

$sql = "Select count(*) from table_name";

$stmt = OCIParse($conn, $sql);
if(!$stmt) {
echo "

ERROR - Could not parse SQL statement.

";
exit;
}

/*
如果你输入了一个错误的SQL语句或者出现了另一个错误,你将看到“无法分析SQL”的错误提示,$stmt现在就是一个定义语句。
现在执行你的分析语句
*/

OCIExecute($stmt);

/*
语句现在已经被执行了,但是正如你所看到的,没有结果标志符从OCIExecute()返回,由OCIParse()返回的结果包含了所有Oracle所需要的信息。
现在开始选择这个查询的结果,$total rows[0]将会包含一个数。如果没有行被返回,则显示错误并退出脚本的运行。
*/

OCIFetchInto($stmt, &$total_rows);

if ( !$total_rows[0] ) {
echo "

Error - no rows returned!

";
exit;
}

/*
这段代码是可选择的但将会使返回的结果更清楚,显示一个类似于“总共有15条记录,现在显示的是第4条到第15条结果。”的注释
*/

$begin =($offset+1);
$end = ($begin+($limit-1));
if ($end > $total_rows[0]) {
$end = $total_rows[0];
}

echo "There are $total_rows[0] results.
n";
echo "Now showing results $begin to $end.

n";

/*
现在是言归正传的时候了,释放原先的语句标识然后建立SQL语句.分析并执行SQL语句,
写代码时需注意:不同于MYSQL,Oracle在SQL语句中不支持限制语句。就本身而言,有很多方法选择特定的行,最好的方法是把选择的结果放在一个临时表里或者“缓冲“所有的结果。这样的方法已经超出了这篇教程的范围了。我们将使用一个更简单的方法,在下面将进一步的解释。
*/
OCIFreeStatement($stmt);

$sql = "Select * from table_name";

$stmt = OCIParse($conn, $sql);

if(!$stmt) {
echo "

ERROR - Could not parse SQL statement.

";
exit;
}

OCIExecute($stmt);

/*
现在显示结果。最简单的做法是在结果集合中使用这个循环。HTML将用来显示最终的结果,但是这个范例非常的简单,所以我们一个也不会用。
编写代码时注意:如上述规定的,在Oracle里是无法限制的,所以我们必须通过在全部的结果集取出我们想要的和终止我们已经有的结果来做这个循环。
在这一小节里,我们将会有几种不同的方法,实际上应该有更好的方法来写这个代码。但是我认为我的这种方法不太容易读懂,它确实运行有效。
*/

$i=0;
$j=0;
while( OCIFetchInto($stmt, &$result_array) ) {
if ($i>=$offset) {
if ($j for ($k=0; $kecho $result_array[$k]." ";
}
echo "
";
$j++;
}
}
$i++;
}
echo "
";

/*
结果将显示在当前的页面里,现在是给访问者一个使用NEXT/PREV点击到其他页面的时候了
*/

// 计算结果所需的页面数,
$pages = intval($total_rows[0]/$limit);

// $pages 现在是除剩余的部分外总共所需的页面数
if ($total_rows[0]%$limit) {
// has remainder so add one page
$pages++;
}

//在显示的是第一页时不显示PREV的连接
if ($offset!=0) {
$prevoffset=$offset-$limit;
echo "   n";
}

// Now loop through the pages to create numbered links
// ex. 1 2 3 4 5 NEXT >>
for ($i=1;$i// Check if on current page
if (($offset/$limit) == ($i-1)) {
// $i is equal to current page, so don display a link
echo "$i   ";
} else {
// $i is NOT the current page, so display a link to page $i
$newoffset=$limit*($i-1);
echo "
$i   n";
}
}

//检查当前页面是否为最后一页
if (!((($offset/$limit)+1)==$pages) && $pages!=1) {
// Not on the last page yet, so display a NEXT Link
$newoffset=$offset+$limit;
echo "NEXT >>

n";
}

/*
现在我们完成了有关Oracle的任务,所以释放最后的语句标识并且退出。
*/

OCIFreeStatement($stmt);
OCILogoff($conn);

?>



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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles