无法使用PHP和Mysql连接到数据库

时间:2016-05-02 11:56:09

标签: 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;
    }
}
?>

在这里,我试图回显存在的行数,但它没有显示任何内容。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

作为反射的基础,如果使用OOP,则将其用作OOP而不是函数库。

我为你做了一些事情,你只需要像这样使用它:

$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;
    }
}

顺便问一下prepare&amp; bindParam&amp; execute&amp; get_result