SQL / PHP将字符串转换为float或int

时间:2012-05-02 19:36:56

标签: php sql floating-point 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或float?

我试过

convert(int, i.total)

那并没有用:(

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

你可以尝试

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