2011-08-23 113 views
0

你們可以給我任何提示,爲什麼字符串值不傳遞給查詢。下面是代碼:字符串MYSQL CASE問題

Array對查詢中使用:

Array ([16] => 41 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => buu [30] => bauadaskd) 

代碼:

foreach ($arrAnswers as $id => $answer) 
{ $update .= sprintf("WHEN '%d' THEN '%d' ", $id, $answer); 
} 
echo $update .= "END WHERE a_question IN ($ids) AND a_user = '{$_SESSION['current_user_id']}'"; 

輸出

UPDATE answers SET a_answer = CASE a_question WHEN '16' THEN '41' WHEN '17' THEN '0' WHEN '18' THEN '0' WHEN '19' THEN '0' WHEN '20' THEN '0' WHEN '21' THEN '0' WHEN '22' THEN '0' WHEN '23' THEN '0' WHEN '24' THEN '0' WHEN '25' THEN '0' WHEN '26' THEN '0' WHEN '27' THEN '0' WHEN '28' THEN '0' WHEN '29' THEN '0' WHEN '30' THEN '0' END WHERE a_question IN (16,17,18,19,20,21,22,23,24,25,26,27,28,29,30) AND a_user = '19 

正如你可以看到指數29和30在輸出中等於0,即使它們在數組中有自己的值。

回答

2

你的問題就在這裏

$更新= sprintf的( 「當 '%d',然後 '%d'」,$ ID,$答案)。

應該

$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer); 
+0

謝謝這是我沒有檢查過的唯一的東西。 – Boris

2

您試圖格式化爲與%d整數。您應該使用%s字符串:

$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer); 
+0

謝謝J0HN它的工作原理 – Boris

2

您的通話sprintf()錯誤格式化字符串,使用%s字符串。 %d是數字:

$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer);