3

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

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

我正在使用 PHP 和 mysqli。

谢谢你。

4

3 回答 3

2

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

于 2010-06-10T02:45:08.050 回答
1

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();
?>
于 2011-02-16T00:57:26.667 回答
0

您可以使用此代码

SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE table_name = 'yourtablename replace your table name' AND COLUMN_NAME = 'tablecolumnname replace your table column name';


在运行主查询之前使用它,然后在获取数据类型之后执行操作。

于 2018-11-26T07:11:56.803 回答