How to get an integer from MySQL as integer in PHP?

前端 未结 9 668
情话喂你
情话喂你 2020-12-31 03:26

When data is returned from MySQL, it is automatically returned as strings, regardless of the MySQL data type.

Is there any way to tell MySQL/PHP to maintain the data

9条回答
  •  梦毁少年i
    2020-12-31 04:11

    If you want to get the values ordening by Price, you can change the Data Type to FLOAT from column (ex: price)

    EX:

    TABLE eletronics

    price        id
    ----------------
    55.90        1
    40.33        2
    10.60        3
    1596.90      4
    56.90        5
    

    PHP:

    $sql = $pdo->query("SELECT * FROM eletronics ORDER BY price DESC");
    

    //result

    1596.90      4
    56.90        5
    55.90        1
    40.33        2
    10.60        3
    

    //The order is being per prices //OBS: You can change the dots with "," using the str_replace() //result: 1596,90 - 56,90 - 55,90 - 40,33 - 10,60 etc.

提交回复
热议问题