方案一(检查安装的mysql扩展)如果使用的为mysqli,应改为mysqlnd(如果php的安装方式为编译:重新编译php,如果为yum安装的php则yum安装php-mysqlnd,不做介绍)
使用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);