简体   繁体   English

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

[英]SQL / PHP converting a string to float or int

I have this sql query.. 我有这个SQL查询..

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

and I ran it in php and echo out the total in a foreach loop like so.... 我在php中运行它并在foreach循环中回显总数,就像这样....

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

and all the totals returned string 并且所有总计都返回了string

how would I convert the i.total in my sql query to int or float? 如何将我的sql查询中的i.total转换为int或float?

i've tried 我试过了

convert(int, i.total)

and that didnt work :( 那不起作用:(

php handles everything (that's not an array/dictionary) as a string unless you specifically tell it not to with something like (int)$row['total'] . php将所有内容(不是数组/字典)作为字符串处理,除非您明确告诉它不要使用类似(int)$row['total']

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

You could try 你可以试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM