注意:未定义的变量:connection

时间:2018-04-02 18:54:25

标签: php mysql xampp localhost undefined-variable

我正在开发一个我在登录页面上工作的网络应用程序。我已经成功开发了登录功能并且工作正常但是当我将Web应用程序(代码和文件)复制到另一台PC以运行相同的登录脚本(php)时,新的login.php脚本显示以下错误

  

注意:未定义的变量:第7行的C:\ xampp \ htdocs \ mydirectory \ login.php中的连接

注意:我正在使用XAMPP,我已将MySQL数据库导入到我的新电脑的phpMyAdmin

当我直接运行db.php脚本时,它显示"已连接"但是当我运行login.php脚本时,它会显示所描述的错误"未定义的变量:连接"

在旧电脑上一切正常,但是当我在新电脑中运行完全相同的代码时,会抛出此错误

我的数据库连接(db.php)文件位于

之下
<?php

$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "mydbname";

foreach($db as $key => $value) {

define(strtoupper($key), $value);

}

$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

 if($connection) {

 echo "Connected";

}else{
  die("QUERY FAILED" . mysqli_error($connection));
  }
?>

我的login.php文件如下:

<?php session_start(); ?>
<?php include "db.php"; ?>

<?php 

    if(isset($_POST['login'])) {

        $pin = mysqli_real_escape_string($connection,$_POST['pin']);
        $password = mysqli_real_escape_string($connection,$_POST['password']);

        $query = mysqli_query($connection,"SELECT * FROM users WHERE pin = '$pin' AND password = '$password' ");

            while($row = mysqli_fetch_array($query)) {

                $user_pin = $row['pin'];
                $user_password = $row['password'];

                $_SESSION['pin']  = $user_pin;
                $_SESSION['password']  = $user_password;
                $_SESSION['id']   = session_id();
                $_SESSION['login_type'] = "user";

                header("Location:home.php");    

       }
            // } else {

            //  echo '<script>alert("User Id or Password is wrong");window.location.assign("index2.php");</script>';

            // }


    }

 ?>

0 个答案:

没有答案