如何从PHP中的oracle中获取所选数据

时间:2015-12-27 00:01:42

标签: php oci

我想通过一个html表单从我的oracle数据库中获取一些数据到php中,我用一个存在于列中的字符串进行搜索。我有以下PHP代码,搜索表单没有任何回报:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[A-Z0-9]+/", $_POST['name'])){
$name=$_POST['name'];
// Connects to the XE service (i.e. database) on the "localhost" machine
$conn = oci_connect('user', 'pwd', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT  deejays.name,available_dates.data,available_dates.venue,available_dates.location FROM deejays,available_dates WHERE deejays.name LIKE '%W&W%' and pk_id=fk_id');
oci_execute($stid);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

}
else{
echo  "<p>Please enter a search query</p>";
}
}
}
?>
<body>
</body>
</html>

我想要的结果是,即: enter image description here

1 个答案:

答案 0 :(得分:0)

您需要修改查询以包含可绑定参数。以下是来自oci_bind_name $sql = 'SELECT last_name FROM employees WHERE department_id = :didbv ORDER BY last_name'; $stid = oci_parse($conn, $sql); $didbv = 60; oci_bind_by_name($stid, ':didbv', $didbv); oci_execute($stid); while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) { echo $row['LAST_NAME'] ."<br>\n"; } 函数的example 4的一部分,您需要根据自己的应用进行调整:

{{1}}

该文档包含对绑定是什么,如何操作以及它对应用程序性能和安全性的重要性的非常好的描述。