2013-10-28 48 views
1

我有一個表像下面:在phpmyadmin和mysql控制檯中工作時,查詢的行爲很奇怪。

目標:

target_id | territory_id | emp_id_tar | target_week   | target | 
15  | 22   | 61   | 2013-10-20/2013-10-26 | 50000 | 
17  | 22   | 63   | 2013-10-20/2013-10-26 | 2000 | 
18  | 22   | 64   | 2013-10-20/2013-10-26 | 150000 | 

正如你可以看到有一個target_week列,其中在這些天的員工應該做到那麼多的量。

現在我試着用下面的查詢:

SELECT * 
FROM targets 
WHERE emp_id_tar =61 
AND MONTH(SUBSTRING_INDEX(target_week, '/', 1)) = MONTH(CURRENT_DATE()) 
AND MONTH(SUBSTRING_INDEX(target_week, '/', -1)) = MONTH(CURRENT_DATE()) 

得到一個特定僱員的特定月份的所有目標金額。以上給出了在phpMyAdmin和mySQL控制檯中的輸出。但是,當我用PHP代碼中使用它,它拋出我像下面的錯誤,

(!) Warning: Division by zero in C:\wamp\www\kurl-on-fe\rest\init.php on line 75 

和PHP代碼如下,我已經證明了線的評論沒有75,

$q = $this->connection->prepare('SELECT * FROM targets WHERE emp_id_tar =61 AND MONTH(SUBSTRING_INDEX(target_week, '/', 1)) = MONTH(CURRENT_DATE()) AND MONTH(SUBSTRING_INDEX(target_week, '/', -1)) = MONTH(CURRENT_DATE())'); 

// above line is line number 75 
$bo = 61; 
$q->bind_param('i', $bo); 
$q->execute(); 
$res = $q->get_result(); 
$temp = array(); 
while($row = $res->fetch_assoc()) { 
    $temp[] = $row; 
} 
return json_encode($temp); 

我我真的很困惑什麼和我在哪裏做錯了?請幫我解決這個問題。還有一件事是我使用2013-10-20/2013-10-26這個結構來開始和結束本週。所以我用substring_index()方法分別得到日期。

回答

3

代替你單引號用雙引號

'/'"/"

$q = $this->connection->prepare('SELECT * FROM targets 
     WHERE emp_id_tar =61 AND 
     MONTH(SUBSTRING_INDEX(target_week, "/", 1)) = MONTH(CURRENT_DATE()) 
     AND 
     MONTH(SUBSTRING_INDEX(target_week, "/", -1)) = MONTH(CURRENT_DATE())'); 
1

嘗試這個

$q = $this->connection->prepare("SELECT * FROM targets 
WHERE emp_id_tar =61 AND MONTH(SUBSTRING_INDEX(target_week, '/', 1)) = MONTH(CURRENT_DATE()) AND MONTH(SUBSTRING_INDEX(target_week, '/', -1)) = MONTH(CURRENT_DATE())");