MySQL integer field is returned as string in PHP

前端 未结 15 2114
南旧
南旧 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:03

    For mysqlnd only:

     mysqli_options($conn, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
    

    Otherwise:

      $row = $result->fetch_assoc();
    
      while ($field = $result->fetch_field()) {
        switch (true) {
          case (preg_match('#^(float|double|decimal)#', $field->type)):
            $row[$field->name] = (float)$row[$field->name];
            break;
          case (preg_match('#^(bit|(tiny|small|medium|big)?int)#', $field->type)):
            $row[$field->name] = (int)$row[$field->name];
            break;
        }
      }
    

提交回复
热议问题