Craig Smith
Craig Smith

Reputation: 137

Warning: PDOStatement::execute() expects parameter 1 to be array, string

I want to pull 32 values from the database, but not in 32 different transactions. I am getting this warning and I am not grasping what it has a problem with.

$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();
}

Upvotes: 1

Views: 14703

Answers (2)

UWU_SANDUN
UWU_SANDUN

Reputation: 1193

When you are using execute() command on PDO.Then you should be want to use array in execute() if you have parameters.Refer following example.

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

Upvotes: 3

prodigitalson
prodigitalson

Reputation: 60403

PDOStatement::execute takes an array as its first param where the indexes matches up to the indexes or keys for the placeholders.

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

Now given your code i would do the following:

$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.

Upvotes: 3

Related Questions