php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39643 Integers selected from a MySQL-database are treated as strings
Submitted: 2006-11-27 14:51 UTC Modified: 2006-11-27 15:23 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: harmen_php_net at xtremesf dot nl Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.0 OS:
Private report: No CVE-ID: None
 [2006-11-27 14:51 UTC] harmen_php_net at xtremesf dot nl
Description:
------------
When executing the query below, I get the wrong result. Although the database returns a integer, PHP treats it as a string. 

Here the relevant part of phpinfo():

MysqlI Support	enabled
Client API library version 	5.0.27
Client API header version 	5.0.27
MYSQLI_SOCKET 	/tmp/mysql.sock

The version of the MySQL server is 5.0.27

Reproduce code:
---------------
<pre>
<?php
$rDB = mysqli_connect('localhost', 'user', 'password', 'db');
mysqli_select_db($rDB, 'db');

$rResult = mysqli_query($rDB, 'SELECT id FROM producten WHERE id = 4472');

var_dump(mysqli_fetch_array($rResult));

echo "\n---\n";

var_dump(mysqli_fetch_field($rResult));

?>
</pre>

Expected result:
----------------
array(2) {
  [0]=>
  int(4) "4472"
  ["id"]=>
  int(4) "4472"
}

---
object(stdClass)#3 (11) {
  ["name"]=>
  string(2) "id"
  ["orgname"]=>
  string(2) "id"
  ["table"]=>
  string(9) "producten"
  ["orgtable"]=>
  string(9) "producten"
  ["def"]=>
  string(0) ""
  ["max_length"]=>
  int(4)
  ["length"]=>
  int(11)
  ["charsetnr"]=>
  int(63)
  ["flags"]=>
  int(49699)
  ["type"]=>
  int(3)
  ["decimals"]=>
  int(0)
}

Actual result:
--------------
array(2) {
  [0]=>
  string(4) "4472"
  ["id"]=>
  string(4) "4472"
}

---
object(stdClass)#3 (11) {
  ["name"]=>
  string(2) "id"
  ["orgname"]=>
  string(2) "id"
  ["table"]=>
  string(9) "producten"
  ["orgtable"]=>
  string(9) "producten"
  ["def"]=>
  string(0) ""
  ["max_length"]=>
  int(4)
  ["length"]=>
  int(11)
  ["charsetnr"]=>
  int(63)
  ["flags"]=>
  int(49699)
  ["type"]=>
  int(3)
  ["decimals"]=>
  int(0)
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-27 14:57 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 [2006-11-27 15:11 UTC] harmen_php_net at xtremesf dot nl
I think this is a bug, because it is not listed on 
 http://nl3.php.net/manual/en/function.mysqli-fetch-array.php.  

But, if this is not a bug, how do I solve the problem, i.e., how do I get the value returned as the right data type (e.g. integers as integers, not as strings)?
 [2006-11-27 15:23 UTC] tony2001@php.net
This is how it is supposed to work and this is how it works from the very beginning.

>how do I get the value returned as the right data type
>(e.g. integers as integers, not as strings)?

$int = (int)$string;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC