SQL / PHP将字符串转换为浮点数或整数


SQL / PHP converting a string to float or int

我有这个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 或浮点数?

我试过了

convert(int, i.total)

这:(

不起作用

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

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

你可以试试

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