【问题标题】:SQL / PHP converting a string to float or intSQL / PHP将字符串转换为float或int
【发布时间】:2012-05-02 19:36:55
【问题描述】:

我有这个 sql 查询..

select i.invoiceid as transactionid, i.date, i.total, 
'invoice' as transaction_type
from invoice

我在 php 中运行它并在 foreach 循环中回显总数,就像这样......

echo gettype($row['total']) . "<br/>";

所有总数返回string

如何将我的 sql 查询中的 i.total 转换为 int 或 float?

我试过了

convert(int, i.total)

那没用:(

【问题讨论】:

    标签: php sql floating-point int


    【解决方案1】:

    php 将所有内容(不是数组/字典)作为字符串处理,除非您明确告诉它不要使用 (int)$row['total'] 之类的东西。

    http://php.net/manual/en/language.types.string.php#language.types.string.conversion

    【讨论】:

      【解决方案2】:

      你可以试试

      select i.invoiceid as transactionid, i.date, CAST(i.total as int) as total, 
      'invoice' as transaction_type
      from invoice
      

      【讨论】:

      • 这给了我一个 SQL 错误(1064)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多