简体   繁体   English

将以下MySQL查询转换为Codeigniter等效项

[英]Convert following MySQL query to Codeigniter equivalent

I'm very new to CodeIgniter and I have some problem converting MySQL queries to CodeIgniter. 我是CodeIgniter的新手,在将MySQL查询转换为CodeIgniter时遇到一些问题。 I want to update a record in one table with condition on another relational table. 我想用另一个关系表上的条件更新一个表中的记录。

Following is my MySQL query 以下是我的MySQL查询

UPDATE `user` SET `securitytoken` = '71b9b5bc1094ee6eaeae8253e787d654'
WHERE `profileid` IN (SELECT pid FROM `profile` WHERE `email` = 'username@mail.com')    

This works fine in phpMyAdmin, but I wanted the same to be converted to codeigniter update query. 这在phpMyAdmin中工作正常,但我希望将其转换为codeigniter更新查询。 Can anyone help me to convert. 谁能帮我转换。

I'm using CI v3.0.3 in Local WAMP Server. 我在本地WAMP服务器中使用CI v3.0.3。

Thanks in Advance 提前致谢

What I would do is code your query as is using 我要做的是按原样使用您的查询代码

$query = $this->db->query('YOUR QUERY HERE');

and use the escape capabilities to take care of your one or two input values. 并使用转义功能来照顾您的一个或两个输入值。

Pay attention to query bindings, may be useful in your case. 注意查询绑定,在您的情况下可能有用。

See: http://www.codeigniter.com/user_guide/database/queries.html 请参阅: http : //www.codeigniter.com/user_guide/database/queries.html

I would not try to use Active Records, your SQL is a bit too complex for that. 我不会尝试使用Active Records,因此您的SQL有点太复杂了。

Try this 尝试这个

$query = $this->db->query("UPDATE `user` SET `securitytoken` = '71b9b5bc1094ee6eaeae8253e787d654'
WHERE `profileid` IN (SELECT pid FROM `profile` WHERE `email` = 'username@mail.com')");
$result = $query->result_array();
return $result;
$query=$this->db->query("SELECT pid FROM `profile` WHERE `email` = 'username@mail.com'")->row_array();

$this->db->set(`securitytoken`,'71b9b5bc1094ee6eaeae8253e787d654');
$this->db->where_in(`profileid`,$query['pid']);
$this->db->update('user');

//echo $this->db->last_query();
//exit;

Try this...! 尝试这个...!

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

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