dtmtu02882 2017-12-10 17:58
浏览 620

一般错误:1008 OCIStmtExecute:ORA-01008:并非所有变量都绑定

I'm doing a login window and when I count the number of rows affected by the SELECT statement to validate the account and the password I have a problem.

Error: SQLSTATE[HY000]: General error: 1008 OCIStmtExecute: ORA-01008: not all variables bound (ext\pdo_oci\oci_statement.c:159)

Here is the problem (I did this because I need count number of rows when i do SELECT. All this is to login.)

I found this piece of code in Official page of PHP: Page PHP link (Example number 2)

$resultado = $base->query($sql);

        if ($resultado) {

            /* Comprobar el número de filas que coinciden con la sentencia SELECT */
          if ($resultado->fetchColumn() > 0) {

                /* Ejecutar la sentencia SELECT real y trabajar con los resultados */
                echo "<h2>Adelante!!</h2>";

            }
            /* No coincide ningua fila -- hacer algo en consecuencia */
          else {
              print "Ninguna fila coincide con la consulta.";
            }
        }

CODE:

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
        try
        {
            $base = new PDO('oci:dbname=localhost', 'hr', 'hr');
            $base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql= "SELECT * FROM DEPARTMENTS WHERE DEPARTMENT_ID=:login AND MANAGER_ID=:password";

            $resultado = $base->prepare($sql);
            $login = htmlentities(addslashes($_POST["login"])); 
            $password = htmlentities(addslashes($_POST["password"]));
            $resultado->bindValue(":login", $login); 
            $resultado->bindValue(":password", $password);

            $resultado->execute();

            $resultado = $base->query($sql);

            if ($resultado) {

              if ($resultado->fetchColumn() > 0) {
                    echo "<h2>Adelante!!</h2>";
                }

              else {
                  print "Ninguna fila coincide con la consulta.";
                }
            }

        }
        catch(Exception $e)
        {
            die("Error: " .$e->getMessage());
        }

    ?>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dtvnbe1428 2017-12-10 22:32
    关注

    Try

    <?php
    
    $did = 70;
    $mid = 204;
    
    try
    {
        $base = new PDO('oci:dbname=localhost', 'hr', 'hr');
        $base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql= "SELECT * FROM DEPARTMENTS WHERE DEPARTMENT_ID = :did AND MANAGER_ID = :mid";
    
        $resultado = $base->prepare($sql);
        $resultado->bindParam(":did", $did);
        $resultado->bindParam(":mid", $mid);
    
        $resultado->execute();
    
        while ($row = $resultado->fetch(PDO::FETCH_ASSOC)) {
            foreach ($row as $item) {
                echo "$item ";
            }
            echo "
    ";
        }
    
    }
    catch(Exception $e)
    {
        die("Error: " .$e->getMessage());
    }
    
    ?>
    

    The key thing is not using query() since you already call prepare() & execute(). It was the query() that was giving the ORA-1008 since it didn't have values for the bind variables.

    Another thing is don't use addslashes etc with Oracle bind variables. The bind data is always separate from code and should be left as the user submitted.

    Also note I used bindParam.

    Check out the PDO and PDO_OCI examples and tests in the doc and on GitHub. There are some general concepts that may be useful in The Underground PHP & Oracle Manual.

    Finally, use the OCI8 extension, not PDO_OCI. OCI8 is better and has more features.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能