如何在php类中使用条件

时间:2014-03-11 05:38:07

标签: php

我是php(oops)的新手。我在(php)类中使用php条件时感到困惑。 我只是想用类来验证我的PHP表单。有人请帮我让我的剧本工作。

class Connect
{
    public $host = "localhost";
    public $username = "root";
    public $password = "";
    public $database = "cls";
    public $mcon;
    public $mdcon;

    function __construct()
    {
        $con = mysqli_connect($this->host, $this->username, $this->password, $this->database);

        if(!$con)
        {
            die;
        }
        else
        {
            $this->mcon= $con;
            return $this->mcon;
        } 
    }

    function Seldb()
    {
        $condb = mysqli_select_db($this->mcon, $this->database);
        $this->mdcon= $condb;
        return $this->mdcon;     
    } 

    function Ucheck($uname, $upass)
    {
        $check = "select * from ut  WHERE u_name ='$uname' and u_pass ='$upass'";
        if ($uresult= mysqli_query($mdcon,$check))
        {
            $urows= mysqli_num_rows($uresult);
        }   
        if ($urows == 1)
        {
            $_SESSION['uname'] = $uname;
            $_SESSION['auth'] = 1 ;
            $_SESSION['check'] = 'sesscheck' ;
            header('Location:http://google.com/');
        }
        else
        {
            $war = "PLEASE enter username and password again.";  
        }
    }
}

1 个答案:

答案 0 :(得分:3)

应该是

if ($uresult= mysqli_query($this->$mdcon,$check))

而不是

if ($uresult= mysqli_query($mdcon,$check))

您需要使用this关键字,否则会给您undefined variable通知。


重写代码。 (在mysqli_error()中添加了另一个来检查你的查询是否失败)

function Ucheck($uname, $upass)
    {
        $check = "select * from ut  WHERE u_name ='$uname' and u_pass ='$upass'";
        if ($uresult= mysqli_query($this->Seldb(),$check))
        {
            $urows= mysqli_num_rows($uresult);
        } else { mysqli_error($this->Seldb());}  //<----- Added This