在另一个文件中从php调用oracle函数

时间:2018-04-10 20:40:23

标签: php oracle function

请不要将其标记为重复,因为我已经尝试了所有解决方案,但却没有得到我实际需要的内容。

所以这是我的queries.php

class Queries {

public static function getNames($name=":name") {
        $dbUser = "...";
        $dbPass = "...";
        $dbConn = "(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP)(HOST=)(PORT=))(CONNECT_DATA=(SID=)))";  
        $conn = oci_connect($dbUser, $dbPass, $dbConn);
        $sql = oci_parse($conn, "SELECT NAME FROM USERS GROUP BY NAME");
        oci_bind_by_name($sql, ':name', $name);
        oci_execute($sql);
        $results = array();
        while ( false!==($row=oci_fetch_all($sql)) ) {
            $results[ $row[$name] ] = $row;
        }
        return $results;
}

profile.php

<?php
error_reporting(E_ALL);
$name = filter_input(INPUT_GET, 'NAME');
require_once './pages/header.php';
require_once './queries.php';
$department = Queries::getNames($name);
?>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-body">
            <form class="cmxform" id="selectdept" method="POST">
        <legend></legend>
        <label for="department"></label>
        <select class="form-control" name="Id" id="Id">
            <option selected disabled></option>
           <?php

            for ($i = 0; $i < count($department); $i++) {
                echo '<option value="' . $department[$i]['NAME'] . '">' . $department[$i]['NAME'] . '</option>';
            }
            ?>
           </select>   
    </form> 
            <br>
            <br>

        </div>    
    </div>
</div>

<?php
require_once './pages/footer.php';
?>

这里,在for循环中我想调用queries.php中的函数。这没什么好给我的。

1 个答案:

答案 0 :(得分:-1)

您对oci_fetch_all的工作方式做出了错误的假设。读取function documentation表明它需要两个参数,一个语句对象和一个存储结果的数组变量。

按原样迭代结果是没有意义的,因为你只应该调用函数一次来接收所有行。正如您所写,PHP将进入无限循环,因为oci_fetch_all永远不会返回布尔false

您还没有在SQL查询中包含:name占位符。

PHP错误日志应该已经充满了警告,所以一如既往地检查这应该是调试的第一步。

class Queries {
    public static function getNames($name=":name") {
        $dbUser = "...";
        $dbPass = "...";
        $dbConn = "(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP)(HOST=)(PORT=))(CONNECT_DATA=(SID=)))";  
        $conn = oci_connect($dbUser, $dbPass, $dbConn);
        $sql = oci_parse($conn, "SELECT NAME FROM USERS WHERE NAME = :name GROUP BY NAME");
        oci_bind_by_name($sql, ':name', $name);
        oci_execute($sql);
        $results = array();
        oci_fetch_all($sql, $results, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);

        return $results;
    }
}