2015-12-27 41 views
2

我想從我的oracle數據庫中通過一個html表單獲取一些數據到php中,在這裏我通過存在一列的字符串進行搜索。我有以下的PHP代碼和搜索表單retuns什麼:如何從oracle中獲取選定的數據在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

回答

0

您需要修改您的查詢,包括綁定參數。下面是從documentationoci_bind_name功能,你只需要適應,以滿足您的應用程序的example 4部分:

$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"; 
} 

文檔包括的結合就是一個很好的說明,如何做到這一點,爲什麼這對於應用程序性能和安全性非常重要。

相關問題