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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:31:23  来源:igfitidea点击:

How to test if Ci successfully inserted data

phpmysqlcodeigniter

提问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

You can use $this->db->affected_rows()function of codeigniter.

您可以使用$this->db->affected_rows()codeigniter 的功能。

See more information here

在此处查看更多信息

You can do something like this:

你可以这样做:

return ($this->db->affected_rows() != 1) ? false : true;

回答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

在模型文件中