我的解決辦法是通過查詢結果$rs
和得到的鑄造數據作爲返回的ASSOC數組:
function cast_query_results($rs) {
$fields = mysqli_fetch_fields($rs);
$data = array();
$types = array();
foreach($fields as $field) {
switch($field->type) {
case 3:
$types[$field->name] = 'int';
break;
case 4:
$types[$field->name] = 'float';
break;
default:
$types[$field->name] = 'string';
break;
}
}
while($row=mysqli_fetch_assoc($rs)) array_push($data,$row);
for($i=0;$i<count($data);$i++) {
foreach($types as $name => $type) {
settype($data[$i][$name], $type);
}
}
return $data;
}
實例:
$dbconn = mysqli_connect('localhost','user','passwd','tablename');
$rs = mysqli_query($dbconn, "SELECT * FROM Matches");
$matches = cast_query_results($rs);
// $matches is now a assoc array of rows properly casted to ints/floats/strings
MySQL查詢給出的行值不是行類型或任何其他相關信息。只是每個表格單元格中的原始數據。您必須在PHP – 2011-03-16 09:24:26
中對此進行解釋如果您需要列類型,請參閱http://forums.whirlpool.net.au/archive/526795 – RichardTheKiwi 2011-03-16 09:36:02
對於讀者,所選答案包括遍歷結果。 但有一個更好的解決方案。 http://stackoverflow.com/a/1197424/5079380 – 2016-07-03 15:13:34