[英]Could not connect to database using PHP and Mysql
I have an issue.i need to connect my database and fetch the table value.But it is not happening like that.I am explaining my code below.我有一个问题。我需要连接我的数据库并获取表值。但它不是那样发生的。我在下面解释我的代码。
index.php:索引.php:
<?php
session_start();
include_once 'dbcon/DBConnection.php';
$dbobj = new DBConnection();
$dbobj->connect();
if (isset($_REQUEST['msg'])){
$msg = urlencode($_REQUEST['msg']);
}
if(isset($_POST["login"])){
//echo 'hii';exit;
$loginid=htmlspecialchars(trim($_POST['txtname']));
$password =sha1(htmlspecialchars(trim($_POST['pwd'])));
//echo $password;exit;
$admin = $dbobj->adminLogin($loginid,$password);
//echo ($admin->result);exit;
if($admin->result == 2){
$msg ='2';
}
if($admin->result ==1){
$_SESSION["admin_id"] = $admin->adminid;
$_SESSION["admin_name"] = $admin->adminname;
$_SESSION["admin_loginid"] = $admin->adminloginid;
header("location:dashboard.php");
}
}
?>
<script>
function valid()
{
var obj = document.frmlogin;
if(obj.txtname.value == "")
{
alert("Please Enter Username");
obj.txtname.focus();
return false;
}
if(obj.pwd.value == "")
{
alert("Please Enter Password");
obj.pwd.focus();
return false;
}
else
{
return true;
}
}
</script>
<form method="post" name="frmlogin" id="frmlogin" action="" autocomplete="off" class="mt">
<label for="" class="text-uppercase text-sm">Username</label>
<input type="text" placeholder="Username" name="txtname" class="form-control mb">
<label for="" class="text-uppercase text-sm">Password</label>
<input type="password" placeholder="Password" name="pwd" class="form-control mb">
<div class="checkbox checkbox-circle checkbox-info">
<input id="checkbox7" type="checkbox" checked>
<label for="checkbox7">
Keep me signed in
</label>
</div>
<button class="btn btn-primary btn-block" name="login" id="login" type="submit" onClick="return valid();">LOGIN</button>
</form>
DBConnection.php:数据库连接.php:
<?php
class DBConnection{
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
public function connect() {
require_once 'dbcon/config.php';
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if ($con->connect_error)die("Connection failed: ");
// return database handler
return $con;
}
public function adminLogin($loginid,$password){
$admin = new AdminUser();
if(ctype_alnum($loginid)){
$sqllogin=sprintf("select * from ".PREFIX."admin where username='%s' and trim(password)='%s' and status=1",mysqli_real_escape_string($con,$loginid),mysqli_real_escape_string($con,$password));
$dbsql=mysqli_query($con,$sqllogin);
$Num = mysqli_num_rows($dbsql);
echo $Num;exit;
if($Num >0){
if($row=mysqli_fetch_array($dbsql)){
$admin->adminid =htmlspecialchars($row['id']);
$admin->adminname =htmlspecialchars($row['name']);
$admin->adminloginid =htmlspecialchars($row['username']);
$admin->result=1;
}
}else{
$admin->result=2;
}
}else{
$admin->result=2;
}
return $admin;
}
}
?>
Here i am trying to echo the number of rows present but its displaying nothing.Please help me to resolve this issue.在这里,我试图回应存在的行数,但它什么也不显示。请帮助我解决这个问题。
As a base of reflection, if you use OOP, use it as OOP and not as function libs.作为反射的基础,如果您使用 OOP,请将其用作 OOP 而不是函数库。
I started something for you, you just have to use it like this :我为你开始了一些事情,你只需要像这样使用它:
$db = new DBConnection('host', 'user', 'pass', 'database_name');
$db->connect();
$data = $db->adminLogin('login', 'password');
- ——
class DBConnection
{
protected $_host = null;
protected $_user = null;
protected $_pass = null;
protected $_database = null;
protected $_con = null;
public function __construct($host, $user, $pass, $db)
{
$this->_host = $host;
$this->_user = $user;
$this->_pass = $pass;
$this->_database = $db;
}
function __destruct()
{
//$this->close();
}
public function connect()
{
$con = new mysqli($this->_host, $this->_user, $this->_pass, $this->_database);
if ($con->connect_error)
{
die("Connection failed: ");
}
$this->_con = $con;
return $con;
}
public function adminLogin($login, $password)
{
$admin = new AdminUser();
if( ctype_alnum($login) )
{
$sqllogin = sprintf(
"select * from ".PREFIX."admin where username='%s' and trim(password)='%s' and status=1",
mysqli_real_escape_string($this->_con, $login),
mysqli_real_escape_string($this->_con, $password));
$dbsql=mysqli_query($this->_con,$sqllogin);
$Num = mysqli_num_rows($dbsql);
echo $Num;exit;
if($Num >0){
if($row=mysqli_fetch_array($dbsql)){
$admin->adminid =htmlspecialchars($row['id']);
$admin->adminname =htmlspecialchars($row['name']);
$admin->adminloginid =htmlspecialchars($row['username']);
$admin->result=1;
}
}else{
$admin->result=2;
}
}else{
$admin->result=2;
}
return $admin;
}
}
Btw please see about prepare
& bindParam
& execute
& get_result
顺便说一句,请参阅prepare
& bindParam
& execute
& get_result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.