2

我正在尝试运行一个函数来查询数据库表中mba_score最接近变量的数值$runscore。我的函数看起来是这样的:

public function get_player($runscore)
{
   $per = $this
             ->db
         ->select('mba_id, abs($runscore - mba_score) as mba_rate', FALSE)
         ->order_by('mba_rate', 'ASC')
         ->get('nba_scores');
         ->limit(1);

        if ($per->num_rows > 0) {
            $p = $per->row_array();
            return $p['nba_id'];
        }
        return false;           
        }

我得到的错误是:

"Error Number: 1054

Unknown column '$runscore' in 'field list'"

我已经尝试多次编写和重写此段,但未能得到结果。

4

3 回答 3

2

使用双引号:

select("mba_id, abs($runscore - mba_score) as mba_rate", FALSE)

使用单引号,变量 $runscore 不会被其值替换。如果你想使用单引号,你应该使用连接:

select('mba_id, abs('.$runscore.' - mba_score) as mba_rate', FALSE)
于 2013-10-04T11:55:42.577 回答
1

改变:

->select("mba_id, abs($runscore - mba_score) as mba_rate", FALSE) //use doube quote
于 2013-10-04T11:55:38.007 回答
0

您应该在变量中使用 's

 ->select("mba_id, abs('".$runscore."' - mba_score) as mba_rate", FALSE)
         ->order_by('mba_rate', 'ASC')
         ->get('nba_scores');
         ->limit(1);
于 2013-10-04T11:56:48.357 回答