简体   繁体   English

Codeigniter活动记录SQL错误

[英]Codeigniter active record sql error

I am using the get_where() function in codeigniter, and I am getting mysql errors, dependent on what I set the limit and offset too, for example this code, 我在codeigniter中使用了get_where()函数,但我也收到了mysql错误,这取决于我也设置了限制和偏移量,例如此代码,

$this->db->get_where('em_user', $whereArr, 30, 0)->num_rows()

returns a mysql error that looks like this, 返回一个看起来像这样的mysql错误,

Error Number: 1064 错误号:1064

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE email = 'your@emailaddress.com' AND password = 'letmein' LIMIT 1' at line 2 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第2行的'WHERE email ='your@emailaddress.com'AND password ='letmein'LIMIT 1'附近使用

SELECT * WHERE email = 'your@emailaddress.com' AND password = 'letmein' LIMIT 1 SELECT * WHERE email ='your@emailaddress.com'AND password ='letmein'LIMIT 1

However if I run this code, 但是,如果我运行此代码,

$this->db->get_where('em_user', $whereArr, 30, 30)->num_rows()

it seems to run fine, it seems to run fine, it returns no results but I don not get the error(I assume the no results is because there is an offset of 30 and I only have 2 records in my table). 它似乎运行良好,看起来运行良好,它没有返回任何结果,但我没有收到错误(我假设没有结果是因为偏移量为30,并且我的表中只有2条记录)。

The sql that this code produces looks like this, 此代码生成的sql如下所示,

SELECT * FROM (`em_user`) WHERE `email` = 'your@emailaddress.com' AND `password` = 'letmein' LIMIT 30, 30

I dont understand how having a limit of 1 at the end of query can cause so much grief, can anyone enlighten me please? 我不明白在查询结束时限制为1怎么会引起如此多的悲伤,任何人都可以启发我吗?

I think this error not related to your line of code 我认为此错误与您的代码行无关

$this->db->get_where('em_user', $whereArr, 30, 0)->num_rows()

because in the error message it's appear LIMIT 1 but in your code you limit 30 因为在错误消息中它显示为LIMIT 1,但是在您的代码中您限制为30

anyway you can try this line instead of get_where 无论如何,您可以尝试此行而不是get_where

$this->db->where($whereArr)->limit(30,0)->get('em_user');

and note this line will return num rows not records 并注意此行将返回num行而不是记录

Also you can view the query to be sure if it's right or not by add this line after your get_where or query 您也可以通过在get_where或查询之后添加此行来查看查询,以确保查询正确与否

die($this->db->last_query());

The line 线

SELECT * WHERE email = 'your@emailaddress.com' AND password = 'letmein' LIMIT 1

has no FROM clause. 没有FROM子句。 I assume it should be: 我认为应该是:

SELECT * from em_user WHERE email = 'your@emailaddress.com' AND password = 'letmein' LIMIT 1

$this->db->get_where('em_user', $whereArr, 30, 30)->num_rows() won't get any results. $this->db->get_where('em_user', $whereArr, 30, 30)->num_rows()将不会得到任何结果。 num_rows() would give the you the number of results. num_rows()将为您提供结果数。

$this->db->select('*');
$this->db->from('em_user');
$this->db->where('email',$email);
$this->db->where('password',$password);
$this->db->limit('30');
$query=$this->db->get();
$result=$query->result();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM