2014-01-27 38 views
0

我班的方法能正常工作。不要给我一些错误信息,但根本不起作用。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数据库连接正确定义($ _ PDO财产),还谁负责与数据库comunicate实例。 ($ _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%。

+0

不应用户引述串 –

+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(); 
+0

Tnx男人很多你的问题!你已经帮助我准备好声明! –