Mysql Retrieve data as numeric not string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zionlion
    New Member
    • Sep 2007
    • 16

    Mysql Retrieve data as numeric not string

    PHP code
    How can I retrieve elements of an array in numeric format?
    Look at this code:[code=php]
    $query = "SELECT price FROM $tbl_name";
    $result = mysql_query($qu ery)
    or die(mysql_error ());



    while($row = mysql_fetch_row ($result)) {
    //$rowNum++;
    $test[]=$row;
    }[/code]

    Let's say the column price has three elements: 33, 47 and 65.
    Please pay attention since the elements of column price are numbers I have to retrieve them as numbers.
    Outside the while loop I need to to have the elements of $test array as:[code=php]
    $element1= 33;
    $element2= 47;
    $element3=65;[/code]
    if I use gettype like;
    echo gettype($elemen t1);
    it should NOT SAY "string" that's a NO NO
    They should be numbers.
    Can anyone help me with this? Please read the question and the code carefully before you answer.
    Thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Zion.

    MySQL fetches string values from the database. Nothing you can do about it.

    But PHP is very good about being able to cast types on the fly, so even though these values are 'strings', you can still perform arithmetical operations on them.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Try use the available casting / convertion function.

      Comment

      Working...