MySQL integer field is returned as string in PHP

前端 未结 15 2117
南旧
南旧 2020-11-22 16:17

I have a table field in a MySQL database:

userid INT(11)

So I am calling it to my page with this query:

\"SELECT userid FR         


        
相关标签:
15条回答
  • 2020-11-22 17:07
    $mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, TRUE);
    

    Try this - worked for me.

    0 讨论(0)
  • 2020-11-22 17:11

    In my project I usually use an external function that "filters" data retrieved with mysql_fetch_assoc.

    You can rename fields in your table so that is intuitive to understand which data type is stored.

    For example, you can add a special suffix to each numeric field: if userid is an INT(11) you can rename it userid_i or if it is an UNSIGNED INT(11) you can rename userid_u. At this point, you can write a simple PHP function that receive as input the associative array (retrieved with mysql_fetch_assoc), and apply casting to the "value" stored with those special "keys".

    0 讨论(0)
  • 2020-11-22 17:13

    MySQL has drivers for many other languages, converting data to string "standardizes" data and leaves it up to the user to type-cast values to int or others

    0 讨论(0)
提交回复
热议问题