Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL登录系统_Php_Sql_Authentication_Mysqli - Fatal编程技术网

Php SQL登录系统

Php SQL登录系统,php,sql,authentication,mysqli,Php,Sql,Authentication,Mysqli,我编写了这个登录系统,但每当我尝试使用数据库表中包含的唯一用户名和密码登录时,就会被重定向到index.php?error=sqlerror。我检查了代码是否有拼写错误,但没有。这可能是数据库连接的问题吗?我使用MAMP。我已经检查了数据库,它显示了成功消息,因此它似乎正在工作。你知道我做错了什么吗?谢谢大家! 数据库连接(文件名:dbh.inc.php) 登录PHP代码(文件名:login.inc.PHP) 我认为问题出在您的文件dbh.inc.php中,您创建了连接$conn=mysqli\

我编写了这个登录系统,但每当我尝试使用数据库表中包含的唯一用户名和密码登录时,就会被重定向到index.php?error=sqlerror。我检查了代码是否有拼写错误,但没有。这可能是数据库连接的问题吗?我使用MAMP。我已经检查了数据库,它显示了成功消息,因此它似乎正在工作。你知道我做错了什么吗?谢谢大家!

数据库连接(文件名:dbh.inc.php)

登录PHP代码(文件名:login.inc.PHP)


我认为问题出在您的文件dbh.inc.php中,您创建了连接$conn=mysqli\u connect($servername、$dBUsername、$dBPassword、$dBName)然后您也关闭它mysqli\u close($conn)

因此,当您在login.inc.php中使用$conn时,您的连接已关闭。您需要做的是在dbh.inc.php中编写一个函数,返回一个实时连接(不要调用close),使用该函数执行DB查询/插入,然后关闭连接

一个可重用的数据库类可以编写(函数式)如下

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

trait DBInfo {
  protected $servername = "127.0.0.1";
    protected $username = "root";
    protected $password = "";
    protected $dbname = "gallerydatabase";
}

class Database{
  use DBInfo;
  function __construct() {}

  function connection(){
      $conn = new mysqli($this->servername, $this->username, 
$this->password, $this->dbname);
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } else{
        $conn->autocommit(FALSE);
        return $conn;
      }
  }

  function select($sql, $fn2bind_takestmt, $fn2process_row_return_result){
    try{
      $result = array();
      $conn = $this->connection();
      $stmt = $conn->prepare($sql);
      $fn2bind_takestmt($stmt);
      $stmt->execute();
      $rowset = $stmt->get_result();
      while ($row = $rowset->fetch_assoc()) {
        $obj = $fn2process_row_return_result($row);
        array_push($result, $obj);
      }
    }catch(Exception $e) {
        $result = NULL;
        throw $e;
    }finally{
      if(isset($rowset))$rowset->close();
      if(isset($stmt))$stmt->close();
      if(isset($conn))$conn->close();
    }
    return $result;
  }

  // You can introduce functions for insert, update and delete as well

}

?>


然后为数据库选择例如登录检查

<?php
function allow_login($user, $pwd){
    $sql = "SELECT count(*) rec_count FROM users WHERE uidUsers=? and pwdUsers=?"
    $db = new Database();
    $result = $db->select($sql,
                function($stmt) use($user, $pwd){
                  $stmt->bind_param("ss", $user, $pwd); 
                },
                function($row){
                  if($row['rec_count'] > 0){// or whatever
                    return TRUE;
                  }
                    return FALSE;
                }
             );
    if(isset($result)){
      return $result[0];
    }
    return $result;
  }  
?>


使用而不是重定向,它会告诉您“我已检查了数据库并显示了成功消息”是什么意思。如果你的意思是“成功”;从dbh.inc.php开始打印,除非在连接过程中出现异常,否则它将一直打印,因为您没有将其放在else部分。我的意思是,正如您所说,我的echo“success”正在打印。你认为我的数据库连接不工作吗?谢谢大家!@蓝色-我希望你参考了我提供的答案,我也解释了你是对的原因!问题是我正在关闭数据库连接。非常感谢。然而,现在当我写下正确的用户名和密码登录时,我会被重定向到index.php?error=ErrorPwd。你知道为什么会这样吗?再次感谢你,很抱歉我缺乏技能。
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

trait DBInfo {
  protected $servername = "127.0.0.1";
    protected $username = "root";
    protected $password = "";
    protected $dbname = "gallerydatabase";
}

class Database{
  use DBInfo;
  function __construct() {}

  function connection(){
      $conn = new mysqli($this->servername, $this->username, 
$this->password, $this->dbname);
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } else{
        $conn->autocommit(FALSE);
        return $conn;
      }
  }

  function select($sql, $fn2bind_takestmt, $fn2process_row_return_result){
    try{
      $result = array();
      $conn = $this->connection();
      $stmt = $conn->prepare($sql);
      $fn2bind_takestmt($stmt);
      $stmt->execute();
      $rowset = $stmt->get_result();
      while ($row = $rowset->fetch_assoc()) {
        $obj = $fn2process_row_return_result($row);
        array_push($result, $obj);
      }
    }catch(Exception $e) {
        $result = NULL;
        throw $e;
    }finally{
      if(isset($rowset))$rowset->close();
      if(isset($stmt))$stmt->close();
      if(isset($conn))$conn->close();
    }
    return $result;
  }

  // You can introduce functions for insert, update and delete as well

}

?>

<?php
function allow_login($user, $pwd){
    $sql = "SELECT count(*) rec_count FROM users WHERE uidUsers=? and pwdUsers=?"
    $db = new Database();
    $result = $db->select($sql,
                function($stmt) use($user, $pwd){
                  $stmt->bind_param("ss", $user, $pwd); 
                },
                function($row){
                  if($row['rec_count'] > 0){// or whatever
                    return TRUE;
                  }
                    return FALSE;
                }
             );
    if(isset($result)){
      return $result[0];
    }
    return $result;
  }  
?>