2013-10-04 166 views
2

我試圖運行一個函數,該函數查詢最接近變量$runscore的數值mba_score的數據庫表。我的功能看起來是這樣的:選擇最接近變量的值

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'" 

我已經試過寫作和重寫該段的次數並沒有能夠得到一個結果。

回答

2

使用雙引號:

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

單引號,變量$ runscore沒有與它的價值代替。如果你想使用單引號,你應該使用串聯:

select('mba_id, abs('.$runscore.' - mba_score) as mba_rate', FALSE) 
+1

記住逃脫'$這個 - > DB->逃生輸入($ runscore)' – machineaddict

1

變化:

->select("mba_id, abs($runscore - mba_score) as mba_rate", FALSE) //use doube quote 
0

你應該用「在你的變量

->select("mba_id, abs('".$runscore."' - mba_score) as mba_rate", FALSE) 
     ->order_by('mba_rate', 'ASC') 
     ->get('nba_scores'); 
     ->limit(1);