我的代码在尝试连接到网站时会产生以下错误,而我正试图弄清楚如何解决它:
连接错误:SQLSTATE [HY000] [2002]
无法建立连接,因为//目标计算机主动拒绝它。(!)致命错误:
在null中调用成员函数prepare() 第53行的C:\ wamp64 \ www \ Allaboutphp \ Class.User.php 调用堆栈
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; 尝试 { $ this-> conn = new PDO(" mysql:host ="。$ this-> host。&#34 ;; dbname ="。$ this-> db_name,$ this-> username,$ this-> password); $ this-> conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); } catch(PDOException $ exception) { echo"连接错误:" 。 $ exception->的getMessage(); } 返回$ 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'>×</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'>×</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();
}