PHP使用mysqli查询数据无数值型问题

使用PHP连接mysql查询数据结果返回的全为string

新建一张user表
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
# 插入数据
INSERT INTO `user` VALUES ('1', 'aaa', '11');

$mysqli = mysqli_connect('127.0.0.1', 'root', 'root', 'user');

if ($mysqli->connect_errno) {
    echo "数据库连接失败", $mysqli->connect_error;
    exit();
}
$query = "select * from user";
$result = $mysqli->query($query);
$row = $result->fetch_array(1);
echo '<pre>';
var_dump($row);

使用mysqli_options
$mysqli = mysqli_connect('127.0.0.1', 'root', 'root', 'user');
//mysqli_options($mysqli, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
$mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
if ($mysqli->connect_errno) {
    echo "数据库连接失败", $mysqli->connect_error;
    exit();
}
$query = "select * from user";
$result = $mysqli->query($query);
$row = $result->fetch_array(1);
echo '<pre>';
var_dump($row);

详见官方文档http://php.net/manual/zh/mysqli.options.php

一条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注