2014-10-03 79 views
-1

我想下面的代碼檢查數據庫中的用戶存在和回聲結果根據output.But我越來越「致命錯誤:方法名稱必須是一個字符串......... .... line「。所以,代碼中出了什麼問題。有什麼建議麼?檢查數據庫的用戶存在

<?php 

$user_email="[email protected]"; 


try 
{ 
    /*** connect to database ***/ 
    /*** mysql hostname ***/ 
    $mysql_hostname = '127.0.0.1'; 

    /*** mysql username ***/ 
    $mysql_username = 'root'; 

    /*** mysql password ***/ 
    $mysql_password = ''; 

    /*** database name ***/ 
    $mysql_dbname = 'something'; 


    /*** select the users name from the database ***/ 
    $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username,  $mysql_password); 
} 
catch(PDOException $e) 
{ 
echo $e->getMessage(); 
} 



if ($this->$dbh()) { 
     // check if username or email already exists 
     $query_check_user_email = $this->$dbh->prepare('SELECT user_email FROM users   WHERE user_email=:user_email'); 
        $query_check_user_email->bindValue(':user_email', $user_email,  PDO::PARAM_STR); 
     $query_check_user_email->execute(); 
     $result = $query_check_user_email->fetchAll(); 

     // if username or/and email find in the database 
     // TODO: this is really awful! 
     if (count($result) > 0) { 
      echo "exists!"; 
      } 
     } else { 
      echo "non existant"; 
} 

?> 

是否有建議?

+0

請放下行號,如果可能的話,提出錯誤的行。 – 2014-10-03 13:55:01

+1

向我們展示整個異常消息。 – 2014-10-03 13:55:58

+0

這是什麼? 'if($ this - > $ dbh()){'?只要下降,如果阻止 – Ghost 2014-10-03 13:56:32

回答

0

$dbh必須用來代替$this->dbh

if ($dbh) { 
     // check if username or email already exists 
     $query_check_user_email = $dbh->prepare('SELECT user_email FROM users  
               WHERE user_email=:user_email'); 
     $query_check_user_email->bindValue(':user_email', 
              $user_email, PDO::PARAM_STR); 
     $query_check_user_email->execute(); 
     $result = $query_check_user_email->fetchAll(); 

     // if username or/and email find in the database 
     // TODO: this is really awful! 
     if (count($result) > 0) { 
      echo "exists!"; 
      } 
     } else { 
      echo "non existant"; 
} 
?> 
2

第一:

$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username,  $mysql_password); 

這將是你的PHP塊/文件/無論範圍的變量。你並沒有真正將其設置爲成員,因爲你不在班上。

第二個:$this->$dbh()

您不在類成員方法的範圍內。沒有$this你的代碼在哪裏。

嘗試使用簡單:$dbh

0

也許這有助於

$stmt = $dbh->prepare("SELECT * FROM user WHERE user_email = :email;"); 
$stmt->bindParam(":email", $email); 
$stmt->execute(); 
$result = $stmt->fetchAll(PDO::FETCH_BOTH); 

BTW我寧願調用表的用戶,而不是用戶。

相關問題