2

Is there a my.cnf (or php.ini) option/settings which will tell php's mysqli extensions to either return the data with the correct data types (ie: int/float/string) or just string?

I ask because I have 2 instances of identical code (unmodified) on 2 different servers, both using php7 and mysql (mariaDB). However, it seems that one returns the query results as all strings while the other returns the query results with the correct data types.

Calls are along the lines of:

$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR;
$mysqli - new mysqli('p:'. $host, $user, $pass, $name);
$mysqli->set_charset('utf8');

$query = "SELECT fieldInt, fieldString FROM mytable";
$result = $mysqli->query($query);
$dataSet = array();
while($row = $result->fetch_assoc()){
   $dataSet[] = $row;
}

echo json_ecnode($dataSet);

Please note, i'm aware of the options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1); option and understand that it will likely fix the problem. What i'm wanting to know is if there are ANY OTHER settings/options which might affect the data type returned as I have 2 identical instances which are returning data in different types.

David
  • 16,246
  • 34
  • 103
  • 162
  • http://php.net/manual/en/mysqli-result.fetch-field.php can tell you the data type seen by mysqli. See the 1st comment for data type codes. If they reflect reality you migth want to look further down on the php side of things. Just a thought – Louis Loudog Trottier May 22 '17 at 20:53
  • And also: https://stackoverflow.com/questions/14180981/how-do-i-make-sure-that-values-from-mysql-keep-their-type-in-php – Dharman Oct 16 '20 at 21:58

1 Answers1

1

Not all SQL numeric types can be represented as PHP int or float types.

For example, how would you store an UNSIGNED INTEGER from SQL as a PHP int?

I don't know why two instances which are "identical" as you say are behaving differently. I suspect they aren't as identical as you think they are.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828