我正在尝试使用Oracle和PHP抛出实现一些错误处理。如果我试图将该语句插入到已经存在PK的DB表中,则不会执行insert操作,但它会返回数据已添加,而实际情况并非如此。需要一些关于Error HAndling 的帮助
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>";
}
?>
您计算的是语句标识符$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