0

I select an integer value from a table and I try to increment it by one but the result is something else.

 TABLE
 id            index  -> BIG INT(20)
 ---------- ----------
 1           191000000000003

<?php 
    $sql='SELECT index from table where id='1'';
    $q=$this->db->query($sql);
    $r=$q->row->array();
    $index= $r['index'];
    $id=$index+1;
    echo $id;    // returns 1.91E+14 
    echo $index; // returns 191000000000003 

    $id=(int)$transfer_index+1;
    var_dump($id); // returns (float) 2147483648
    var_dump($transfer_index); // returns string

  expected result: 191000000000004
?>
xyonme
  • 395
  • 1
  • 7
  • 24
  • 1
    You do realize that `index` is a MySQL reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - and just for the heck of it, so is `table` but I doubt that's just an example name. – Funk Forty Niner Oct 13 '14 at 03:54
  • I just use an anonymous column_name in my question. @Fred-ii- – xyonme Oct 13 '14 at 03:55
  • Can you post a `var_dump` of `$id` and `$index`. I'm almost certain that `$index` is a string, and is typecasted to a float when you add 1. The "`1.91E+14`" is just standard form number syntax. If "standard form" is the issue, please have a look at [this question](http://stackoverflow.com/questions/1471674/why-is-php-printing-my-number-in-scientific-notation-when-i-specified-it-as-00) – Scopey Oct 13 '14 at 03:56
  • I take posted code and diagrams literally/for face value ;) – Funk Forty Niner Oct 13 '14 at 03:56
  • @Scopey Yes , you are right. updated my question. How do i get my expected result? – xyonme Oct 13 '14 at 04:01
  • @Fred-ii- Yes Fred. sorry for misleading you. :) – xyonme Oct 13 '14 at 04:02
  • Sidenote: `$sql="SELECT index from table where id='1'";` or `$sql='SELECT index from table where id=1';` if `id` is an `int`. I'm sure you got a syntax error thrown back at you, right? If not, then you're not checking for errors. – Funk Forty Niner Oct 13 '14 at 04:03
  • http://stackoverflow.com/questions/7151236/php-how-to-convert-bigint-from-int-to-string – user3380585 Oct 13 '14 at 04:05
  • @Fred-ii- There is no error occurrence Fred. This integer value has been changed into float.. – xyonme Oct 13 '14 at 04:05
  • See the link just above that `user3380585` left. There stands to be a solution in there. – Funk Forty Niner Oct 13 '14 at 04:07
  • when I plus one , it becomes a float type number. The page is telling us how to change a number become a string. @user3380585 – xyonme Oct 13 '14 at 04:18

2 Answers2

0

Try this:

$sql="SELECT (index + 1) as ind from table where id='1'";
$q=$this->db->query($sql);
$index=$q->row()->ind;
echo $index;
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
0

Maximum integer value on your machine is less that the value stored in the BIG_INT field.

Jehad Keriaki
  • 545
  • 5
  • 10