2014-01-27 128 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男人很多你的問題!你已經幫助我準備好聲明! –