简体   繁体   English

如何在 PHP oci8 中使用准备好的语句和绑定参数

[英]How to use prepared statements and bound parameters in PHP oci8

So using prepared statements and bound parameters is the suggested way for writing sql statements.因此,使用准备好的语句和绑定参数是编写 sql 语句的建议方式。 Oci8 manual does not describe how to do it with prepared statements. Oci8手册没有描述如何使用准备好的语句来做到这一点。

Below is how to return the next row from a query as an object, but it's not the best practice as the query string can contain a where col = $PHPvariable下面是如何将查询中的下一行作为对象返回,但这不是最佳实践,因为查询字符串可以包含where col = $PHPvariable

<?php

    $conn = oci_connect('hr', 'welcome', 'localhost/XE');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }

    $select_sql= oci_parse($conn, 'SELECT id, description FROM mytab');
    oci_execute($select_sql);

    while (($row = oci_fetch_object($select_sql)) != false) {
        // Use upper case attribute names for each standard Oracle column
        echo $row->ID . "<br>\n";
        echo $row->DESCRIPTION . "<br>\n"; 
    }

    oci_free_statement($stid);
    oci_close($conn);

    ?>

Yes it's possible to use oci8 parameterized query for your sql statements.是的,可以对您的 sql 语句使用 oci8 参数化查询。

oci_bind_by_name binds a PHP variable to the Oracle bind variable placeholder bv_name. oci_bind_by_name将 PHP 变量绑定到 Oracle 绑定变量占位符 bv_name。 Binding is important for Oracle database performance and also as a way to avoid SQL Injection security issues.绑定对于 Oracle 数据库性能很重要,也是避免 SQL 注入安全问题的一种方式。

Binding reduces SQL Injection concerns because the data associated with a bind variable is never treated as part of the SQL statement.绑定减少了 SQL 注入问题,因为与绑定变量关联的数据永远不会被视为 SQL 语句的一部分。 It does not need quoting or escaping.它不需要引用或转义。

Read more here .在这里阅读更多。

 <?php

    $conn = oci_connect("hr", "hrpwd", "localhost/XE");
    if (!$conn) {
        $m = oci_error();
        trigger_error(htmlentities($m['message']), E_USER_ERROR);
    }

    $sql = 'SELECT last_name FROM employees WHERE department_id = :dpid ';

    $stid = oci_parse($conn, $sql);
    $didbv = 60;

    oci_bind_by_name($stid, ':dpid ', $didbv);
    oci_execute($stid);

    while (($row = oci_fetch_object($stid)) != false) {
        echo $row->last_name ."<br>\n";
    }


    oci_free_statement($stid);
    oci_close($conn);

    ?>

后除去空间:dpid线14上正确的语法=> oci_bind_by_name($stid, ':dpid', $didbv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM