2014-09-23 120 views
0

我试图让我的登录主页工作。在第一次我建立了我的查询与MySQL,但我总是得到错误“查询是空的”,所以我决定改为mysqli。我现在得到错误“警告:mysqli_query()期望至少2个参数”。 如何在类“database2”的方法“connect()”中调用我的“mysqli_query($ db,$ string)”中的第一个参数“$ db”以使其工作? 我试着用 “$结果= $这个 - > cxn->查询($查询)” 但后来我得到了错误“致命错误:调用未定义的方法::的Database2查询()mysqli面向对象编程

<?php 

class Login 
{ 
    private $username; 
    private $password; 
    private $cxn; // Database object. 

    function __construct($username, $password) 
    { 
     // Set data 
     $this->setData($username, $password); 

     // connect to db 
     $this->connectToDb(); 

     //get Data 
     $this->getData(); 
    } 

    private function setData($username, $password){ 

      $this->username = $username; 
      $this->password = $password; 

      } 

    private function connectToDb(){ 

      include 'models/database2.php'; 
      $vars = "include/vars.php"; 
      $this->cxn = new Database2($vars); 

      } 

    private function getData(){ 

      $query ="SELECT 'username', 'password' FROM 'users' WHERE 'username' = '$this->username' 
            AND 'password' = '$this->password'"; 

      $result = mysqli_query($query) or die(mysql_error()); 
      $num_row = mysqli_num_rows($result); 

       if ($num_row>1) { 
        return TRUE; 
       }else{ 
         throw new Exception("The query was not successful!"); 
        } 
       } 

     function close(){ 
     $this->cxn->close_connect(); 
      } 

} 
?> 

类的Database2 :

<?php 


class Database2{ 


    private $host; 
    private $user; 
    private $password; 
    private $database; 

    function __construct($filename){ 
     if(is_file($filename)){ 
      include $filename; 
     }else{ 
      throw new Exception("Error Processing Request"); 
      } 
     $this->host  = $host; 
     $this->user  = $user; 
     $this->password =$password; 
     $this->database =$database; 

     $this->connect();  
     } 

     public function connect(){ 
         // connect to the server. 
        $db = new mysqli($this->host, $this->user, $this->password);  
         if ($db->connect_errno) { 
          die("We are sorry, you could not be connected to the server, 
          plaese check your connection setting!"); 
         }else{ 
          echo "You are connected to the database"; 
         } 
        } 


     public function close_connect(){ 
     mysql_close(); 

     } 
} 


?> 

我感谢所有帮助

+0

什么是_Project面向programming_?你的意思是_Object Oriented Programming_? – Barmar 2014-09-23 10:42:41

+0

wtf正在用那个'mysql_close();'? – 2014-09-23 12:17:08

+0

您不会将密码作为纯文本存储到数据库中。你也不需要以纯文本的方式将它们保持为私人成员 - 如果你需要保持它们在那里。通常,在验证时只需要密码哈希值,也就是当您将数据库作为后端查询时,检查用户是否提供了正确的密码。 – hakre 2014-09-23 18:35:10

回答

-2

如果您使用的程序的风格,你必须设置和使用“$ DB”变量在DATABASE2类 设置变量在连接功能:

$this->db = new mysqli($this->host, $this->user, $this->password); 

然后,你可以使用$ DB在DATABASE2对象

$result = mysqli_query($this->cxn->db, $query); 
+1

mysql_error不能与mysqli一起使用。另外,这不是程序风格。另一个提示是:首先分配一个局部变量来保持代码的灵活性,而不管它来自哪里。 PHP中的变量相当便宜,使你的代码变得更好...。 – hakre 2014-09-23 12:43:58

+0

我的mysql_error错误,对不起。但这是mysqli的程序性用法,在面向对象的风格中,你不必使用“link”参数(http://php.net/manual/en/mysqli.query.php) 编辑:修复mysql_error – MadLefty 2014-09-23 14:02:08

+0

你谈论的链接*是*一个对象。由于该过程需要一个对象,该过程本身是面向对象的。一旦理解,最好只写'$ result = $ this-> cxn-> db-> query($ query);'。函数名称“mysqli_query”的使用只对你的代码行非常有用(非常有可能)。你也刚刚删除了任何类型的错误处理,这是不可建议的。由于数据库是远程服务,错误处理非常重要。 – hakre 2014-09-23 18:29:05