mysqli和字段类型

问题描述:

我想知道是否有一个简单的方法从mysql表中获取具有“正确”数据类型的数据?我的意思是,如果字段类型是例如INT或SMALLINT,可以将这些类型直接传递给PHP作为整数?mysqli和字段类型

我做了一些搜索,发现mysqli_fetch_fields,但对于SMALLIT类型是2,对于INT 3等。它可以这样做,但它看起来相当笨拙的解决方法。有没有更好的方法?

我使用PHP和mysqli。

谢谢。

+0

你在哪里找到它的类型是什么样的价值观上的列表? – PeterJCLaw 2011-06-21 21:12:46

最直接的方法是在mysqli调用之上构建您自己的数据库处理程序,并为您执行此操作。

http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php

<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5"; 

if ($result = $mysqli->query($query)) { 

    /* Get field information for column 'SurfaceArea' */ 
    $finfo = $result->fetch_field_direct(1); 

    printf("Name:  %s\n", $finfo->name); 
    printf("Table: %s\n", $finfo->table); 
    printf("max. Len: %d\n", $finfo->max_length); 
    printf("Flags: %d\n", $finfo->flags); 
    printf("Type:  %d\n", $finfo->type); 

    $result->close(); 
} 

/* close connection */ 
$mysqli->close(); 
?>