需要帮助诊断php错误吗
我的代码在尝试连接到站点时产生以下错误,我正在尝试找出解决方法: 连接错误:SQLSTATE[HY000][2002]需要帮助诊断php错误吗,php,mysql,database-connection,prepare,Php,Mysql,Database Connection,Prepare,我的代码在尝试连接到站点时产生以下错误,我正在尝试找出解决方法: 连接错误:SQLSTATE[HY000][2002] 无法建立连接,因为//目标计算机主动拒绝连接 (!)致命错误: 在中对null调用成员函数prepare() C:\wamp64\www\allabouthp\Class.User.php,第53行 调用堆栈 时间记忆功能位置 10.0012249224{main}()…\Index.php:0 2.0062 297264用户->登录()…\Index.php:16 //db
无法建立连接,因为//目标计算机主动拒绝连接 (!)致命错误: 在中对null调用成员函数prepare() C:\wamp64\www\allabouthp\Class.User.php,第53行 调用堆栈
时间记忆功能位置
10.0012249224{main}()…\Index.php:0
2.0062 297264用户->登录()…\Index.php:16
//dbconfig.php conn=null; 尝试 { $this->conn=new-PDO(“mysql:host=“.this->host.”dbname=“.this->db_name,$this->username,$this->password”); $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常); } 捕获(PDO异常$exception) { echo“连接错误:”.$exception->getMessage(); } 返回$this->conn; } } ?> index.php:
登录|编码框架
&时代;
对不起此帐户未激活。请转到您的收件箱并激活它。
&时代;
错误的细节强>
登录。
登录
*************************************************
dbconfig.php
Class.User.php:
错误消息指出您的连接被拒绝,因此我将查看您的$conn凭据,其中任何一个都是已声明编辑的问题。标题行太长/太详细。这是个问题。格式太糟糕了。要了解使问题可读的选项,请单击文本输入窗口上方编辑工具栏中的“帮助”链接。你的支撑风格浪费了大量的垂直空白,需要在小窗口中滚动太多才能看到你所有的代码,所以我压缩了它,这样可以一次看到更多的代码,人们可以更快地阅读/理解它。最后,异常/错误指向index.php,因此我首先列出了该代码。请更加努力地清楚地提出好的问题,这样人们可以更好地帮助你。谢谢你的帮助这是我第一次使用这个网站。我添加了我的dbconfig.php页面,如果有人可以帮助我进行进一步的分析。HappyCarts您是否有时间查看我的dbconfig文件,看看您是否可以为我指出正确的方向。现在,我甚至不知道该用谷歌搜索什么来弄清这件事的真相。再次感谢你。
<?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;
}
}
?>
<?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();
}