2017-10-19 127 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