Php 用于检查sql server中是否存在值的函数

Php 用于检查sql server中是否存在值的函数,php,sql-server,pdo,Php,Sql Server,Pdo,嘿嘿, 我需要一个php函数来检查表单输入的值是否已经存在于数据库sql-server-PDO中,并返回TRUE或FALSE 我试图这样做,但我陷入困境,并没有找到一个解决办法在互联网上。 你能给我一个如何威胁上述情况的提示吗 function check_code($code) { GLOBAL $handler; $code = check_input($code); try{ $query2 = $handler -> prepare("SELECT co

嘿嘿,

我需要一个php函数来检查表单输入的值是否已经存在于数据库sql-server-PDO中,并返回TRUE或FALSE

我试图这样做,但我陷入困境,并没有找到一个解决办法在互联网上。 你能给我一个如何威胁上述情况的提示吗

function check_code($code) {
GLOBAL $handler;
$code = check_input($code);
        try{
      $query2 = $handler -> prepare("SELECT code from stock where code = :code");
      $query2 -> execute(array(
                    ':code' => $code
                    )); 
return  >???<
        }
        catch(PDOException $e){
            echo $e -> getMessage();
      }  }
返回类似于row_countresult>0的内容

我以前从未使用过sql server,但我使用过PDO很多次,基本上这就是签入PDO的方式

<?php
function check_code($code)
{
    GLOBAL $handler;

    $code = check_input($code);
    try {
        $query2 = $handler->prepare("SELECT code from stock where code = :code");
        $query2->execute(array(':code' => $code));
        $results = $query2->fetchColumn();
        if (count($results) > 0) {
            echo "exists";
        } else {

            echo "does not exist";
        }
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
}
?>

注意:避免使用全局变量

我使用了您的示例,即使这些值不存在,它也会返回count$results=1=exists。确定尝试更改fetchColumn并替换为fetch或fetchall@IonutP。