SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry convert db var to int

    Hello, SP folks!

    I am extracting a number form db. After accessing the value, I want to set it to a var.

    But the catch here is that this var only accepts int type. If I assign an int as below:

    PHP Code:
    $somevar 2009
    This turns out to be OK.

    But when I assign the number (not int) extracted from db, as follows, it does not work:

    PHP Code:
    $somevar $table['somefield']; 
    I tried using following methods to have $somevar converted to int type, but there were no promising results:
    PHP Code:
    //1
    $somevar = (int)$table['somefield'];
    //2
    $somevar intval($table['somefield']);
    //2a - arg 2 is optional
    $somevar intval($table['somefield'],10); 
    $somevar's supposed to contain expiration time for cookie, if it helps helping my out.

    Thanks SitePointers!

  2. #2
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,

    somefield is varchar type in database???

    well there is quick trick .

    $table['somefield']*1 : I believe this will surely convert the string (number) to int.

  3. #3
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by priti View Post
    Well,

    somefield is varchar type in database???

    well there is quick trick .

    $table['somefield']*1 : I believe this will surely convert the string (number) to int.
    Hi,

    Interestingly enough, the field is of type INT.

    I'll try out your method, hope it works! thanks!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All three of your methods will work correctly, assuming $table['somefield'] contains a numeric string. var_dump($table['somefield']); to make sure.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •