php 和 sql 中的错误致命错误:未捕获类型错误:mysqli_query():参数 #1 ($mysql) 必须是 mysqli、字符串类型

问题描述 投票:0回答:2

尝试通过获取详细信息来让用户登录,但不起作用,请检查此

这是我的连接代码

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "atm";

$conn = new mysqli($server ,$username ,$password ,"atm");

if(!$conn =""){
    echo "connection is established<br>";
}
?>

我的主要文件代码是

<?php
    require_once "../configmain.php";

    if(isset($_POST['login'])){
      $username = $_POST["username"];
      $password = $_POST["password"];
      $adminqry = mysqli_query($conn,"select 'admin_id' from 'admin_details' where STATUS = '1' and username = '$username' and password = '$password';")or die (mysqli_error($con));
      while($admin = mysqli_fetch_object($adminqry)){
        $adminid = $admin -> admin_id;
      }

      session_start();
      $_SESSION['adminid'] = $adminid;
      $adminid = $_SESSION['adminid'];
      echo $adminid;

      if(!$adminid){
        header("Location: ./admin/adminmain.php");
  exit();
      }
      else{

      echo "login error";
      }
    }

?>

检查我的连接是否正常工作,如果我的数据库正常工作,但它也正常工作,正在与 xampp 服务器一起工作,请帮助我

php mysqli
2个回答
0
投票

你的问题是这一行:

if(!$conn ="")

您没有检查

$connection
是否为空,您实际上是将其设置为空字符串。

尝试将您的支票更改为:

if(!$conn->errno)

这将检查连接对象上是否有任何非零错误号。


0
投票

试试这个

第 1 步:用下面给定的代码替换您的连接文件

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "atm";

$conn = new mysqli($server ,$username ,$password ,$database);

?>

第 2 步:用下面的代码替换您的主文件代码

     <?php
            require_once "../configmain.php";
        
            if(isset($_POST['login'])){
              $username = $_POST["username"];
              $password = $_POST["password"];
              $adminqry = mysqli_query($conn,"select 'admin_id' from 'admin_details' where STATUS = '1' and username = '$username' and password = '$password';")or die (mysqli_error($conn));
        
              if(mysqli_num_rows($adminqry) == 1){
                  $admin = mysqli_fetch_object($adminqry);
                  $adminid = $admin -> admin_id;
                  session_start();
                  $_SESSION['adminid'] = $adminid;
        
                  if(isset($_SESSION['adminid'])){
                    header("Location: ./admin/adminmain.php");
                    exit();
                  }else{
                    echo "Login error";
                  }
        
              }else{
                echo "User not found.";
              }
          }
        
 ?>

  //Notes:
    //(1) Check your connection file path is proper in require_once.
    //(2) I consider you want to redirect header("Location: ./admin/adminmain.php"); here if login success.
© www.soinside.com 2019 - 2024. All rights reserved.