Search code examples
phpmysqlmysqlisqldatatypes

mysqli returning bits and ints as string


I have a project made in flex, using PHP data services to access an SQL server database, and I need to convert it to MySQL, I have changed all my PHP services from sqlsrv to mysqli.
Something like this:

$this->connection = mysqli_connect(SERVER,USERNAME,PASSWORD,DATABASE); // Start Connection
$ssqlstring = "select * from Users";
$runssql = mysqli_query($this->connection, $ssqlstring);
while($user = mysqli_fetch_array($runssql)) 
{
  $users[$user["ID"]] = $user;
}
return $users;

It worked fine on sqlsrv but with mysqli it returns to flex the INT, BIT, or DATE values as string

the main Datatypes on MySQL are INT, VARCHAR, BIT(1), DATETIME, DATE (same as sqlsrv)
and on the flex the return types are mainly as object[]


Solution

  • from the manual

    Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.

    There are relatively few native types in PHP, in short, possibles are INT, FLOAT and STRING (where STRING is the default for all the rest, like BIT and DATE etc.). PDO is a bit better at using more close-to-actual types, but still defaults to strings a lot. In mysqli, you could use mysqli_result_fetch_fields to get the type of field returned, and cast it to a desired format, see also the MYSQLI_TYPE_* constants.