【问题标题】:How to catch sql exceptions in php如何在php中捕获sql异常
【发布时间】:2017-09-26 07:53:10
【问题描述】:

我在php中写rest apis,但是在api中插入一些空值时会抛出错误。它将sql查询显示到响应页面中,如何阻止它显示,是什么在这种情况下最好的异常处理。我对 php 有点陌生,所以任何帮助表示赞赏。 下面是我写的查询示例代码。

  $limit = " LIMIT ".$post_data['offset'].",".$post_data['limit'];
    $sql = "SELECT record_id from ";
    $shareResult= $this->db->query($sql)->result_array();
    if(empty($shareResult[0]['record_id']) || $shareResult[0]['record_id'] != 0){
        $query="SELECT *";

        $result= $this->db->query($query);
        if($result->num_rows()>0){
            $res1=$this->db->query($qry)->result_array();
            $res[0]=$res1[0];
            $res[1]=$result->result_array();
            return $res;
        }
    }else if($shareResult[0]['record_id'] === 0){
        return false;
    }

【问题讨论】:

标签: php codeigniter exception-handling


【解决方案1】:

如果你在谈论 Codeigniters 默认调试选项,你应该仔细看看@your db config

应该会出现类似的东西

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database_name',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array()
);

查看db_debug 并将其设置为false

【讨论】:

    猜你喜欢
    • 2017-02-23
    • 2011-03-31
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    相关资源
    最近更新 更多