2011-11-25 101 views
-1

我想從數據庫中提取32個值,但不是32個不同的事務中。我得到這個警告,我並沒有把握它有什麼問題。警告:PDOStatement :: execute()期望參數1是數組,字符串

$team = "SELECT teamname FROM teamnames WHERE"; 
for ($j = 1; $j <= 32; ++$j) { 
    teamid == "$j"; 
    if ($j != 1) { 
     $team .= ","; 
    } 
    if ($j == 32) { 
     $team .= "\")"; 
    } 
} 

$teamselect = $db->prepare($team); 
$teamselect->execute($team); 
for ($y = 1; $y <= 32; ++$y) {  
    $_SESSION["team$y"] = $teamselect->fetchColumn(); 
} 
+4

'$ team'不是準備好的語句。 – nickb

+0

由於您沒有使用佔位符,因此您不需要爲該選擇準備好聲明。 另一方面,你可能想要清理你的代碼。有一個名爲$ teamid的變量,你沒有使用。結果SQL也沒有多大意義(SELECT teamname FROM teamnames WHERE ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)) 。那是你的實際代碼嗎? – mpratt

+0

那麼,teamid是數據庫中的一列,這些值是自動遞增的,因此我試圖從數據庫中的每個teamid拉出隊名。 –

回答

2

PDOStatement::execute帶有一個數組作爲它的第一個參數,其中所述索引相匹配的索引或鍵的佔位符。

$team="SELECT teamname FROM teamnames WHERE teamname.id = ?"; 
$stmt = $pdo->prepare($team); 
$stmt->execute(array($yourteamid)); 

現在給你的代碼中,我將做到以下幾點:

$ids = range(1,32); 

$team = sprintf(
    'SELECT teamnames.teamname FROM teamnames WHERE teamnames.teamid IN (%s)', 
    implode(',', array_fill(1, count($ids), '?')) 
); 

$teamselect=$db->prepare($team); 
$teamselect->execute($ids); 

// then do whatever with your results. 
1

當您使用上PDO​​命令。然後,你應該要使用數組中​​如果你有parameters.Refer以下例。

$userQuery = $dbh->prepare("SELECT * FROM user where id = ?"); 
$userQuery->execute(array($_SESSION['user'])); 
$userData = $userQuery->fetch(PDO::FETCH_ASSOC); 
相關問題