php - 在 Codeigniter 4 中添加 mysqli 选项

标签 php codeigniter mysqli

我的数据库中有几个字段是 DECIMAL(10,2) 或 INT 值,当我返回结果时,所有内容都以字符串形式返回。

例子:

    $sql = "SELECT id, operacion_id FROM operaciones";
    $query = $this->db->query($sql);
    $result = $query->getResultArray();

    return $this->response->setStatusCode(200)
                    ->setContentType('application/json')
                    ->setJSON($result);

然后返回:

[{
    "id": "31",
    "operacion_id": "ES-2021-0031"
},
{
    "id": "30",
    "operacion_id": "ES-2021-0030"
}]

我试着直接放在system\Database\MySQLi\Connection.php connect() 方法中:

$this->mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

这很好用,但我知道它升级不安全。

最佳答案

您应该能够将此选项应用于数据库对象的 mysqli 属性。

// run this after your DB connection has been initialized
$this->db->mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
// and then run your queries
$query = $this->db->query($sql);

使用准备好的语句时,您不必应用此设置,因为准备好的语句总是以正确的类型返回数据。

关于php - 在 Codeigniter 4 中添加 mysqli 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68421980/

相关文章:

php - 我的 sql 查询中的语法错误(ON DUPLICATE KEY UPDATE)

php - 您是否在 form_validation 之后发布输入值?

mysql 行在 codeigniter 中未正确更新

php - 使用 PHP 将 MySQL 从本地主机恢复到实时服务器

php - symfony 形式 : Date is not valid

PHP 查找对象在数组中具有最高值

php - 如何解决非法字符串偏移错误并显示以下代码片段中的表格?

PHP + MySQL : How to know when an UPDATE query des not match the WHERE sentence?

php - mysqli 绑定(bind)变量

PHP UTF-8 不工作