如何从sql中选择数据作为int而不是字符串?

y3bcpkx1  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(316)

我有这个密码:

$TargetPlayerPosition = $GLOBALS['DATABASE']->uniquequery("SELECT total_rank FROM ".STATPOINTS." WHERE `id_owner` = '". $this->_fleet['fleet_target_owner'] ."';");

问题是我不能把+10加到 $TargetPlayerPosition ,因为它是字符串。但在db里, total_rank 是一个int。

j2cgzkjk

j2cgzkjk1#

不确定查询后如何从resultset检索值。通常,应该从db中的整型字段中获取整型值。如果没有,那么可以用php转换值。

$v = (int)$v;
 // Or.
 $v = intval($v);

您可以使用如下查询在查询时动态添加数字:

"SELECT (total_rank+10) AS total_rank FROM ".STATPOINTS." WHERE ..."

即使字段是字符串类型,也可以执行以下操作:

"SELECT (CONVERT(total_rank, SIGNED INTEGER)+10) AS total_rank FROM ".STATPOINTS." WHERE ..."
pokxtpni

pokxtpni2#

<?php 
$TargetPlayerPosition = $GLOBALS['DATABASE']->uniquequery("SELECT total_rank FROM ".STATPOINTS." WHERE `id_owner` = '". $this->_fleet['fleet_target_owner'] ."';");
/* conversion to int */
$rank = (int) $TargetPlayerPosition;
/* do the required arithmetic */
/* $rank + 10 will work now */
?>

相关问题