php 如何测试Ci是否成功插入数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9729241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if Ci successfully inserted data
提问by Michael Grigsby
In Ci, I've got the following function. How do I test that the query successfully inserted without error's?
在 Ci 中,我有以下功能。如何测试查询是否成功插入而没有错误?
public function postToWall() {
$entryData = $this->input->post('entryData');
$myChurchId = $this->session->userdata("myChurchId");
$this->db->query("INSERT IGNORE INTO wallPosts (entryData, entryCreationDateTime, wpChurchId)
VALUES('$entryData', NOW(), '$myChurchId')");
}
回答by Chirag
回答by Nadeem Khan
You can also do it using Transactionslike this:
您也可以使用这样的交易来做到这一点:
$this->db->trans_start();
$this->db->query("INSERT IGNORE INTO wallPosts (entryData, entryCreationDateTime, wpChurchId)
VALUES('$entryData', NOW(), '$myChurchId')");
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return "Query Failed";
} else {
// do whatever you want to do on query success
}
Here's more infoon Transactions in CodeIgniter!
这是有关 CodeIgniter 中事务的更多信息!
回答by teenage vampire
if you are using bootstrapon codeignitertry flash message or simply redirect
如果您在codeigniter上使用引导程序,请尝试使用 flash 消息或简单地重定向
$added = $this->your_modal->function_reference($data);
if ($added) {
$this->session->set_flashdata('success', 'Added successfully.');
redirect('home');
} else {
$this->session->set_flashdata('error', 'Something wrong.');
redirect('home');
ON CONTROLLER
在控制器上
回答by deya tri
I prefer use
我更喜欢使用
echo $this->db->affected_rows();
in models file
在模型文件中