Mysqli and field types

I would like to know if there is an easy way to get data from mysql tables with the "correct" data types? What do I mean if a field type, for example, INT or SMALLINT, can I pass these types directly to PHP as integers?

I searched a bit and found mysqli_fetch_fields, but for SMALLIT type 2, for INT 3 and so on. It can be done this way, but it looks rather awkward. Is there a better way?

I am using PHP and mysqli.

Thank.

+3
source share
2 answers

The easiest way is to create your own database handler on top of the mysqli calls that do this for you.

+2
source

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();
?>
+1

Source: https://habr.com/ru/post/1749323/


All Articles