我的数据库中有几个字段是 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/