PDO ::这个查询出了什么问题?

时间:2014-01-27 11:31:56

标签: php pdo

我班上的方法可以正常使用。不要给我一些错误信息,但根本不起作用。

        public function query($value)
            {
                $this->__error = FALSE;

                        $sql = "SELECT * FROM users WHERE username = ".Input::input($value);

                        if ($this->__query = $this->__pdo->query($sql))
                        {
                             $this->__result = $this->__query->fetchAll(PDO::FETCH_OBJ);
                    $this->__count = $this->__query->rowCount(); //Here is the problem

                        }


                    else {
                    $this->__error = TRUE;
     }   
     return $this;
                }

     public function count()
            {
                return $this->__count;
  }

但我不会编写全类代码,我提到PDO DataBase连接已正确定义( $ _ pdo property ),也是实例。负责与数据库通信。 ( $ _instance property )。输入类。

这是我的index.php(某种注册形式):

    <?php 

    spl_autoload_register(function($class) //Load all class in project
    {
       require_once 'class/'.$class.'.php';
    }
        );

    $user = DataBase_class::instance()->query("username"); //username is the name of textbox

    if ($user->count())
    {
        echo 'User exist';
    }
    else echo 'User not exist';
?> 

结果是“用户不存在”,尽管用户存在100%。

1 个答案:

答案 0 :(得分:3)

你忘记了引号

$sql = "SELECT * FROM users WHERE username = '".Input::input($value) . "'";

但你应该考虑使用准备好的陈述..

$stmt = $this->__pdo->prepare("SELECT * FROM users WHERE username = :name");
$stmt->bindParam(':name', Input::input($value));
$result = $stmt->execute();