2017-10-19 84 views
0
<?php 
class DbOperations 
{ 
    private $con; 

    function __construct() 
    { 
     require_once dirname(__FILE__).'/DbConnect.php'; 

     $db = new DbConnect(); 

     $this->con = $db->connect(); 
    } 

    public function createUser($email, $password, $gender, $dob_year, $dob_month, $dob_day, $time_registered) 
    { 
     if($this->isUserExist($email)) 
     { 
      echo "0"; 
      return 0; 
     } 
     else 
     { 
      $stmt = $this->con->prepare("insert into table_user (`id`, `email`, `password`, `gender`, `dob_year`, `dob_month`, `dob_day`, `time_registered`) values (NULL, ?, ?, ?, ?, ?, ?, ?);"); 

      $stmt->bind_param("sssssss", $email, $password, $gender, $dob_year, $dob_month, $dob_day, $time_registered); 

      if($stmt->execute()) 
      { 
       echo "1"; 
       return 1; 
      } 
      else 
      { 
       echo "2"; 
       return 2; 
      } 
     } 
    } 

    private function isUserExist($email) 
    { 
     $stmt = $this->con->prepare("select id from table_user where email = ?"); 
     $stmt->bind_param("s", $email); 
     $stmt->execute(); 
     $stmt->store_result(); 

     return $stmt->num_rows > 0; 
    } 
} 
?> 

嗨,我想做一個註冊頁面。當我從isUserExists()方法返回true或false時,createUser函數完美工作。看起來像$ stmt-> num_rows> 0;總是返回false,但我看到數據被保存到數據庫中。它應該在用戶註冊時將返回值更改爲true,但它總是返回false。這有什麼問題?php isUserExist always always

+0

什麼'$這個 - > CON組> prepare','$ stmt->的execute()的返回值;'和'$ stmt-> store_result()'?另外,您是否啓用了錯誤報告? (請參閱[如何獲取PHP錯誤以顯示](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)) – ccKep

+0

都是1和1 – Eric

回答

0

你也有語法問題和參數。是「bindParam」,而不是「bind_param」。同樣的問題也出現在插入中。請參閱文檔中的示例:http://php.net/manual/en/pdostatement.bindparam.php

用途:

$stmt = $this->con->prepare("select id from table_user where email = ?"); 
$stmt->bindParam("1", $email, PDO::PARAM_STR); 

或者:

$stmt = $this->con->prepare("select id from table_user where email = :email"); 
$stmt->bindParam(":email", $email, PDO::PARAM_STR); 
+0

這可能是mysqli,而不是PDO。它是[bind_param](http://php.net/manual/de/mysqli-stmt.bind-param.php)。 – ccKep