英文:
Message: Undefined property: CI_DB_mysqli_result::$num_row
问题
我收到错误消息:未定义属性: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_rows() == 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
文件名: models/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
函数: validate
文件: C:\xampp\htdocs\ov400\index.php
行: 315
函数: require_once
英文:
I get error Message:Undefined property: CI_DB_mysqli_result::$num_row
my controller is
<?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');
}
}
}
and my model is
<?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;
}
}
}
and my view is
<?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>
> A PHP Error was encountered
Severity: Notice
> Message: Undefined property: CI_DB_mysqli_result::$num_row
> Filename: models/Login_mod.php
> Line Number: 27
> Backtrace:
> File: C:\xampp\htdocs\ov400\application\models\Login_mod.php
> Line: 27
> Function: _error_handler
> File: C:\xampp\htdocs\ov400\application\controllers\Login.php
> Line: 23
> Function: validate
> File: C:\xampp\htdocs\ov400\index.php
> Line: 315
> Function: require_once
答案1
得分: 1
只是一个语法错误。在模型中,if
子句的正确语句应该是:
if ($query->num_rows() == 1) {
return TRUE;
} else {
return FALSE;
}
如果您想使用三元运算符将此代码压缩为一行,您可以使用:
return ($query->num_rows() == 1) ? true : false;
以上代码将产生相同的结果,并且代码更加简洁。
英文:
Just a syntax error. The correct statement for the if
clause in the model would be
if ($query->num_rows() == 1) {
return TRUE;
} else {
return FALSE;
}
And, if you'd like to compact this code to a one-liner using a ternary operator, you could use:
return ($query->num_rows() == 1) ? true : false;
the above would produce the same result, with a much cleaner code
答案2
得分: 1
不是$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;
}
}
英文:
It's not $query->num_row();
it's $query->num_rows();
Change your validate
To this:
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;
}
}
答案3
得分: 1
更改您的验证函数
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;
}
}
英文:
Change Your Validate function
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;
}
}
答案4
得分: 0
使用此查询:
function validate($username, $password) {
return $this->db->where('username', $username)->where('pass', $password)->get('user')->row_array();
}
英文:
use this query:
function validate($username,$password) {
return $this->db->where('username', $username)->where('pass', $password)->get('user')->row_array();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论