如果您使用mysqlnd作为传输客户端并MYSQLI_OPT_INT_AND_FLOAT_NATIVE
在 mysqli 对象上设置选项,您实际上会得到本机类型。
echo 'phpversion: ', phpversion(), "\n";
$m = new mysqli('localhost', 'localonly', 'localonly', 'test');
$m->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
$m->query('CREATE TABLE soTest (x int)');
$m->query('INSERT INTO soTest (x) VALUES (1),(2)');
$r = $m->query('SELECT x from soTest');
var_dump($r->fetch_assoc());
印刷
php版本:5.3.3
数组(1){
[“x”]=>
整数(1)
}
此功能已在 PHP 5.3 中添加。
为了让您开始使用PDO(如果您选择使用它):
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// the pdo_mysql driver uses emulated prepared statements by default
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// afaik only the mysqlnd client supports native types, with libmysql you'll get only strings/null
echo 'client: ', $pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";
// set up test environment
$pdo->exec('CREATE TEMPORARY TABLE soTest (x int, y varchar(16))');
$pdo->exec("INSERT INTO soTest (x,y) VALUES (1,'a'),(2,null)");
// statement with positional parameter
$stmt = $pdo->prepare('SELECT x,y FROM soTest WHERE x>?');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute( array(0) );
foreach( $stmt as $row ) {
foreach( $row as $col ) {
echo gettype($col), '(', $col, ') ';
}
echo "\n";
}
印刷
client: mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $
integer(1) string(a)
integer(2) NULL()