dt2015 2017-01-05 18:21
浏览 33

需要帮助诊断php错误

My code produces the following error when trying to connect to a site, and I'm trying to figure out how to resolve it:

Connection error: SQLSTATE[HY000] [2002]
No connection could be made because //the target machine actively refused it.

( ! ) Fatal error:

Call to a member function prepare() on null in C:\wamp64\www\Allaboutphp\Class.User.php on line 53 Call Stack

Time Memory Function Location
1 0.0012 249224 {main}( ) ...\Index.php:0
2 2.0062 297264 USER->login( ) ...\Index.php:16


//dbconfig.php

conn = null; try { $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password); $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $exception) { echo "Connection error: " . $exception->getMessage(); } return $this->conn; } } ?>

index.php:


<?php
session_start();
require_once 'class.user.php';
$user_login = new USER();

if($user_login->is_logged_in()!="") {
    $user_login->redirect('home.php');
}

if(isset($_POST['btn-login'])) {
    $email = trim($_POST['txtemail']);
    $upass = trim($_POST['txtupass']);

    if($user_login->login($email,$upass))
    {
        $user_login->redirect('home.php');
    }
}
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Login | Coding Cage</title>
    <!-- Bootstrap -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
    <link href="assets/styles.css" rel="stylesheet" media="screen">
     <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
  </head>
  <body id="login">
    <div class="container">
        <?php 
        if(isset($_GET['inactive']))
        {
            ?>
            <div class='alert alert-error'>
                <button class='close' data-dismiss='alert'>&times;</button>
                <strong>Sorry!</strong> This Account is not Activated Go to your Inbox and Activate it. 
            </div>
            <?php
        }
        ?>
        <form class="form-signin" method="post">
        <?php
        if(isset($_GET['error']))
        {
            ?>
            <div class='alert alert-success'>
                <button class='close' data-dismiss='alert'>&times;</button>
                <strong>Wrong Details!</strong> 
            </div>
            <?php
        }
        ?>
        <h2 class="form-signin-heading">Sign In.</h2><hr />
        <input type="email" class="input-block-level" placeholder="Email address" name="txtemail" required />
        <input type="password" class="input-block-level" placeholder="Password" name="txtupass" required />
        <hr />
        <button class="btn btn-large btn-primary" type="submit" name="btn-login">Sign in</button>
        <a href="signup.php" style="float:right;" class="btn btn-large">Sign Up</a><hr />
        <a href="fpass.php">Lost your Password ? </a>
      </form>

    </div> <!-- /container -->
    <script src="bootstrap/js/jquery-1.9.1.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
  </body>
</html>
*************************************************
dbconfig.php
<?php
class Database
{

    private $host = "localhost";
    private $db_name = "dbtest";
    private $username = "root";
    private $password = "";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

Class.User.php:


<?php
require_once 'dbconfig.php';
class USER {    
private $conn;
public function __construct() {
    $database = new Database();
    $db = $database->dbConnection();
    $this->conn = $db;
}

public function runQuery($sql) {
    $stmt = $this->conn->prepare($sql);
    return $stmt;
}

public function lasdID() {
    $stmt = $this->conn->lastInsertId();
    return $stmt;
}

public function register($uname,$email,$upass,$code) {
    try {                           
        $password = md5($upass);
        $stmt = $this->conn->prepare("INSERT INTO                        tbl_users(userName,userEmail,userPass,tokenCode) 
        VALUES(:user_name, :user_mail, :user_pass, :active_code)");
        $stmt->bindparam(":user_name",$uname);
        $stmt->bindparam(":user_mail",$email);
        $stmt->bindparam(":user_pass",$password);
        $stmt->bindparam(":active_code",$code);
        $stmt->execute();   
        return $stmt;
    } catch(PDOException $ex) {
        echo $ex->getMessage();
    }
}

public function login($email,$upass) {
    try {
        $stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE userEmail=:email_id");
        $stmt->execute(array(":email_id"=>$email));
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

        if($stmt->rowCount() == 1) {
            if($userRow['userStatus']=="Y") {
                if($userRow['userPass']==md5($upass)) {
                    $_SESSION['userSession'] = $userRow['userID'];
                    return true;
                } else {
                    header("Location: index.php?error");
                    exit;
                }
            } else {
                header("Location: index.php?inactive");
                exit;
            }   
        } else {
            header("Location: index.php?error");
            exit;
        }       
    } catch(PDOException $ex) {
        echo $ex->getMessage();
    }
}


public function is_logged_in() {
    if(isset($_SESSION['userSession'])) {
        return true;
    }
}

public function redirect($url) {
    header("Location: $url");
}

public function logout() {
    session_destroy();
    $_SESSION['userSession'] = false;
}

function send_mail($email,$message,$subject) {                      
    require_once('mailer/class.phpmailer.php');
    $mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->SMTPDebug  = 0;                     
    $mail->SMTPAuth   = true;                  
    $mail->SMTPSecure = "ssl";                 
    $mail->Host       = "smtp.gmail.com";      
    $mail->Port       = 465;             
    $mail->AddAddress($email);
    $mail->Username="your_gmail_id_here@gmail.com";  
    $mail->Password="your_gmail_password_here";            
    $mail->SetFrom('your_gmail_id_here@gmail.com','Coding Cage');
    $mail->AddReplyTo("your_gmail_id_here@gmail.com","Coding Cage");
    $mail->Subject    = $subject;
    $mail->MsgHTML($message);
    $mail->Send();
}   
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集