【发布时间】:2016-05-03 13:16:46
【问题描述】:
dbconnect.php
class dbconnect
{
public function connect()
{
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'demo';
$connection = mysqli_connect($host, $user, $pass, $db);
return $connection;
}
}
dao.php
include 'dbconnect.php';
class dao extends dbconnect
{
private $conn;
function __construct()
{
$dbcon = new dbconnect();
$conn = $dbcon->connect();
}
function select($table, $where = '', $other = '')
{
if (!$where = '') {
$where = 'where' . $where;
}
$sele = mysqli_query($this->conn, "SELECT * FROM $table $where $other") or die(mysqli_error($this->conn));
echo $sele;
return $sele;
}
}
controller.php
include 'dao.php';
$d = new dao();
if (isset($_POST['btn_login'])) {
extract($_POST);
$username = $_POST['user_name'];
$pswd = $_POST['pswd'];
$sel = $d->select("users", "email_id = '" . $username . "'AND password='" . $pswd . "'") or die('error from here');
$result = mysqli_fetch_array($sel);
if ($result['email_id'] == $username && $result['password'] == $pswd) {
SESSION_START();
$_SESSION['user_name'] = $result['email_id'];
$_SESSION['message'] = 'Invalid Username Or Password';
header("location:index.php");
} else {
$_SESSION['error'] = 'Invalid Username Or Password';
}
}
我遇到了一个错误
-
警告:mysqli_query() 期望参数 1 为 mysqli,在第 13 行的 /opt/lampp/htdocs/ankit_demo/dao.php 中给出 null
-
警告:mysqli_error() 期望参数 1 为 mysqli,在第 13 行的 /opt/lampp/htdocs/ankit_demo/dao.php 中给出 null
请帮我解决这个问题。
【问题讨论】:
-
$this->conn!==$conn -
主要问题是您没有在构造函数中创建类属性
$conn,而只是一个局部变量。另外,你应该使用__construct(),而不是 php4 风格的构造函数(实际上是一种奇怪的混合) -
那我该如何解决呢?请告诉我我是 php 的新开发人员