MySQL integer field is returned as string in PHP

前端 未结 15 2372
南旧
南旧 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 16:57

    If prepared statements are used, the type will be int where appropriate. This code returns an array of rows, where each row is an associative array. Like if fetch_assoc() was called for all rows, but with preserved type info.

    function dbQuery($sql) {
        global $mysqli;
    
        $stmt = $mysqli->prepare($sql);
        $stmt->execute();
        $stmt->store_result();
    
        $meta = $stmt->result_metadata();
        $params = array();
        $row = array();
    
        while ($field = $meta->fetch_field()) {
          $params[] = &$row[$field->name];
        }
    
        call_user_func_array(array($stmt, 'bind_result'), $params);
    
        while ($stmt->fetch()) {
          $tmp = array();
          foreach ($row as $key => $val) {
            $tmp[$key] = $val;
          }
          $ret[] = $tmp;
        }
    
        $meta->free();
        $stmt->close();
    
        return $ret;
    }
    

提交回复
热议问题