翻译结果: 消息:未定义属性:CI_DB_mysqli_result::$num_row

huangapple go评论77阅读模式
英文:

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

&lt;?php
class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this-&gt;load-&gt;database();
        $this-&gt;load-&gt;model(&#39;Login_mod&#39;);
    }
    function login() {
        $username = $this-&gt;input-&gt;post(&#39;username&#39;);
        $password = $this-&gt;input-&gt;post(&#39;password&#39;);
      
        $result = $this-&gt;Login_mod-&gt;validate($username,$password);
        if (!$result) {
            $msg = &quot;&lt;font color=red&gt; Invalid Username or Password.&lt;/font&gt;&lt;/br&gt;&quot;;
            $data[&#39;msg&#39;] = $msg;
            $this-&gt;load-&gt;view(&#39;login/Login&#39;, $data);
        } else {
            $this-&gt;load-&gt;view(&#39;login/index&#39;);    
        }
    }
}

and my model is

&lt;?php
class Login_mod extends CI_Model {

    function __construct() {
        parent::__construct();
        $this-&gt;load-&gt;database();
    }

    function validate($username,$password) {
       // $username = $this-&gt;input-&gt;post(&#39;username&#39;);
      //  $password = $this-&gt;input-&gt;post(&#39;password&#39;);
        $this-&gt;db-&gt;where(&#39;username&#39;, $username);
        $this-&gt;db-&gt;where(&#39;pass&#39;, $password);
        $query = $this-&gt;db-&gt;get(&#39;user&#39;);
        if ($query-&gt;num_row == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

and my view is

&lt;?php
?&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Login Account&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div&gt;
            &lt;form method=&quot;post&quot;  method=&quot;post&quot; name=&quot;login&quot; action=&quot;&lt;?php echo base_url(&#39;&#39;); ?&gt;Login/login&quot;&gt;
                  &lt;h2&gt;Login Form&lt;/h2&gt;
                  &lt;div &gt;
                      &lt;label for=&quot;username&quot; &gt;Uername: &lt;/label&gt;
                      &lt;input style=&quot;margin-left:2px&quot; type=&quot;text&quot; value=&quot;&quot; name=&quot;username&quot; placeholder=&quot;User Name&quot; id=&quot;username&quot; required=&quot;&quot; /&gt;
                &lt;/div&gt;
                  &lt;/br&gt;
                &lt;div&gt;
                    &lt;label for=&quot;username&quot; &gt;password: &lt;/label&gt;
                    &lt;input type=&quot;password&quot; value=&quot;&quot; name=&quot;password&quot; placeholder=&quot;Password&quot; id=&quot;password&quot; required=&quot;&quot; /&gt;
                &lt;/div&gt;
                  &lt;/br&gt;
                  &lt;div&gt;
                    &lt;input type=&quot;button&quot; id=&quot;btnLogin&quot; value=&quot;Login&quot;&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;

> 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-&gt;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-&gt;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-&gt;num_row(); it's $query-&gt;num_rows();

Change your validate To this:


function validate($username,$password) {
        $this-&gt;db-&gt;where(&#39;username&#39;, $username);
        $this-&gt;db-&gt;where(&#39;pass&#39;, $password);
        $query = $this-&gt;db-&gt;get(&#39;user&#39;);
        if ($query-&gt;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-&gt;db-&gt;where([&#39;username&#39;=&gt; $username, &#39;pass&#39;=&gt; $password]);
 $query = $this-&gt;db-&gt;get(&#39;user&#39;);
 $num = $query-&gt;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-&gt;db-&gt;where(&#39;username&#39;, $username)-&gt;where(&#39;pass&#39;, $password)-&gt;get(&#39;user&#39;)-&gt;row_array();
}

huangapple
  • 本文由 发表于 2020年1月6日 18:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定