我有一個問題,我需要連接我的數據庫並獲取表值。但它不像這樣發生。我正在解釋我的代碼如下。無法連接到使用PHP和Mysql的數據庫
的index.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
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;
}
}
?>
我在這裏想呼應當前行,但它沒有顯示的數量。請幫我解決這個問題。
你是使用mysql_real_escape_string,但你連接使用mysqli,所以在config.php文件中使用mysqli_real_escape_string – Mihai
我已經聲明瞭憑據及其正確。 – satya
您的$ con在連接功能中可見,所以您的其他功能無法看到它,AFAIK。 – Mihai