【问题标题】:Error Handling PHP Oracle Database错误处理 PHP Oracle 数据库
【发布时间】:2016-04-11 04:16:00
【问题描述】:

我正在尝试使用 Oracle 和 PHP 实现一些错误处理。如果我尝试将语句插入到已经存在 PK 的 DB 表中,则不会执行 INSERT - 但它返回数据已添加 - 当它真的不是时 - 需要一些有关错误处理的帮助

require('connection.inc');
require('connection_db.inc');
$conn = db_connect();

$sql = oci_parse($conn,"INSERT INTO Schema.TableA (DOE, Trips) VALUES (:doe, :trp)");

oci_bind_by_name($sql, ':doe', $f1);
oci_bind_by_name($sql, ':trp', $f2);

oci_execute($sql);

<?
if($sql)
{
    echo("Input data has been added<br><br>");
    echo("<a href='link1.php'>View Links</a>");
}
else
{
    echo("Input data has failed");
    echo "</div>";
}

?>

【问题讨论】:

    标签: php database oracle error-handling


    【解决方案1】:

    您正在评估语句标识符 $sql,而不是 oci_execute 调用的结果...

    oci_execute 如果成功则返回 true,如果查询失败则返回 false。见http://php.net/manual/en/function.oci-execute.php

    $conn = oci_connect('hr', 'welcome', 'localhost/XE');
    
    $stid = oci_parse($conn, 'SELECT * FROM employees');
    $result = oci_execute($stid);
    
    if(true === $result){
        // Query successfully executed
        echo "Hooary";
    } else {
        // Something went wrong
        $e = oci_error($stid);
        echo "Error: " . $e['message'];
    }
    

    小提示,从您发布的代码来看,您似乎正在学习 php,如果您想以一种更安全、更轻松的方式与数据库交互,我会说看看 PDO。有一个可用于 PDO 的 oci 驱动程序。

    http://php.net/manual/en/book.pdo.php
    http://php.net/manual/en/ref.pdo-oci.php

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 2017-09-13
      相关资源
      最近更新 更多