查询错误“标识符无效”


Query Error "invalid identifier"

我的数据库搜索表单有问题。该页面在大多数情况下运行良好,但我收到两个错误。

警告:ociexecute(( [function.ociexecute]:ORA-00904:*$search_term*:第 53 行 **/search_results.php 中的标识符无效

警告:ocifetch(( [function.ocifetch]:ORA-24374:在第 70 行的 **/search_results.php 中定义在获取或执行和获取之前未完成

我已经评论了相关的行号。如果有人能告诉我为什么会发生这种情况,我将不胜感激:)

编辑:忘了提及,尽管输入了应该得到结果的搜索词,但表格不显示任何内容

编辑 2:当查询更改为 SELECT * FROM 电话时,它工作正常。它必须与查询有关。

<?php
    $search_term = $_POST['search_box'];
    /* Set oracle user login and password info */
    $dbuser = "**"; /* your login */
    $dbpass = "**"; /* your oracle access password */
    $db = "**"; 
    $connect = OCILogon($dbuser, $dbpass, $db);
    if (!$connect) {
    echo "An error occurred connecting to the database"; 
    exit; 
    }
    /* build sql statement using form data */
    $query = "SELECT * FROM Phones WHERE Name LIKE ".$search_term;
    /* check the sql statement for errors and if errors report them */
    $stmt = OCIParse($connect, $query);
    echo "SQL: $query<br>";
    if(!$stmt) {
        echo "An error occurred in parsing the sql string.'n"; 
        exit; 
    }
    OCIExecute($stmt); //line 53
    ?>
    <h1 class="green">PHP and Oracle databases</h1>
    <h4>Table: <em>Phones</em></h4>
    <div align="center">
    <table width="850" border="0" bgcolor="#339933" cellpadding="5" cellspacing="1">
    <tr bgcolor="#006633">
    <td width="75">ID</td>
    <td width="75">Name</td>
    <td width="100">Brand</td>
    <td width="75">Photo</td>
    </tr>
    <?php
    // Display all the values in the retrieved records, one record per row, in a loop
    while(OCIFetch($stmt)) { //line 70
        // Start a row for each record
        echo("<tr valign=top bgcolor=#ccffcc>");
        // Output fields, one per column
        // Drainage value in column one
        $fg1 = OCIResult($stmt,"ID"); //"ID number";
        echo("<td width=75>");
        echo ($fg1);
        echo("</td>");
        // Aspect value in column two
        $fg2 = OCIResult($stmt,"NAME");//"Name of product";
        echo("<td width=75>");
        echo ($fg2);
        echo("</td>");
        // Temperature value in column three
        $fg3 = OCIResult($stmt,"BRAND");//"Brand of product";
        echo("<td width=75>");
        echo ($fg3);
        echo("</td>");
        // Height value in column four
        $fg4 = OCIResult($stmt,"PHOTO");//"Photo file path"; 
        echo("<td width=75>");
        echo ($fg4);
        echo("</td>");
        // End the row 
        echo("</tr>");
    } 
    // Close the connection
    OCILogOff ($connect); 
    ?>

看起来您的 SQL 命令需要在$search_term内容两边加上引号。

我不知道PHP,但我想它应该是这样的:

$query = "SELECT * FROM Phones WHERE Name LIKE '%".$search_term."%'"; 

请注意添加撇号(SQL 的引号字符(和百分号(LIKE 的通配符(。我不确定您如何在 PHP 中正确引用撇号 ('( 和百分号 (%(,所以您应该检查一下。

(抱歉,我不知道如何在 Oracle 上将其设置为不区分大小写的搜索,您应该将其作为单独的问题发布。