我试图让我的登录主页工作。在第一次我建立了我的查询与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();
}
}
?>
我感谢所有帮助
什么是_Project面向programming_?你的意思是_Object Oriented Programming_? – Barmar 2014-09-23 10:42:41
wtf正在用那个'mysql_close();'? – 2014-09-23 12:17:08
您不会将密码作为纯文本存储到数据库中。你也不需要以纯文本的方式将它们保持为私人成员 - 如果你需要保持它们在那里。通常,在验证时只需要密码哈希值,也就是当您将数据库作为后端查询时,检查用户是否提供了正确的密码。 – hakre 2014-09-23 18:35:10