我收到错误消息:未定义的属性:CI_DB_mysqli_result::$num_row
我的控制器是
<?php
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Login_mod');
}
function login() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->Login_mod->validate($username,$password);
if (!$result) {
$msg = "<font color=red> Invalid Username or Password.</font></br>";
$data['msg'] = $msg;
$this->load->view('login/Login', $data);
} else {
$this->load->view('login/index');
}
}
}
我的模型是
<?php
class Login_mod extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function validate($username,$password) {
// $username = $this->input->post('username');
// $password = $this->input->post('password');
$this->db->where('username', $username);
$this->db->where('pass', $password);
$query = $this->db->get('user');
if ($query->num_row == 1) {
return TRUE;
} else {
return FALSE;
}
}
}
我的观点是
<?php
?>
<html>
<head>
<title>Login Account</title>
</head>
<body>
<div>
<form method="post" method="post" name="login" action="<?php echo base_url(''); ?>Login/login">
<h2>Login Form</h2>
<div >
<label for="username" >Uername: </label>
<input style="margin-left:2px" type="text" value="" name="username" placeholder="User Name" id="username" required="" />
</div>
</br>
<div>
<label for="username" >password: </label>
<input type="password" value="" name="password" placeholder="Password" id="password" required="" />
</div>
</br>
<div>
<input type="button" id="btnLogin" value="Login">
</div>
</form>
</div>
</body>
</html>
遇到 PHP 错误 严重性:通知
消息:未定义的属性:CI_DB_mysqli_result::$num_row
文件名:型号/Login_mod.php
行号:27
回溯:
文件: C:\xampp\htdocs\ov400\application\models\Login_mod.php 行: 27 功能:_error_handler
文件: C:\xampp\htdocs\ov400\application\controllers\Login.php 行: 23 功能:验证
文件: C:\xampp\htdocs\ov400\index.php 行: 315 功能:require_once
只是一个语法错误。模型中if
子句的正确表述是
if ($query->num_rows() == 1) {
return TRUE;
} else {
return FALSE;
}
而且,如果您想使用三元运算符将此代码压缩为单行代码,您可以使用:
return ($query->num_rows() == 1) ? true : false;
上面将产生相同的结果,代码更干净
这不是$query->num_row();
而是$query->num_rows();
将您的validate
更改为:
function validate($username,$password) {
$this->db->where('username', $username);
$this->db->where('pass', $password);
$query = $this->db->get('user');
if ($query->num_rows == 1) {
return TRUE;
} else {
return FALSE;
}
}
更改验证函数
function validate($username,$password) {
$this->db->where(['username'=> $username, 'pass'=> $password]);
$query = $this->db->get('user');
$num = $query->num_rows();
if($num){
return true;
} else{
return false;
}
}
使用此查询:
function validate($username,$password) {
return $this->db->where('username', $username)->where('pass', $password)->get('user')->row_array();
}