簡體   English   中英

SQL / PHP將字符串轉換為float或int

[英]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或float?

我試過了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM