查询错误“无效标识符”

时间:2013-10-10 14:13:23

标签: php sql oracle search

我的数据库搜索表单存在问题。该页面大部分运行良好,但我得到两个错误。

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

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

我评论了相关的行号。如果有人能告诉我为什么会这样,我会非常感激:)

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

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

<?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); 
    ?>

1 个答案:

答案 0 :(得分:1)

看起来您的SQL命令需要围绕$search_term内容的引号。

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

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

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

(抱歉,我不知道如何在Oracle上进行不区分大小写的搜索,你应该将其作为一个单独的问题发布。)