1

Adapting an answer from here to try and pass an array as the parameter for a WHERE clause in MySQL. Syntax seems okay but I'm just getting null back form the corresponding JSON. I think understand what it is supposed to do, but not enough that I can work out where it could be going wrong. The code for the function is;

public function getTheseModulesById($moduleids) {
    require_once 'include/Config.php';
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
    // Check connection
    if (!$con)
      {
        die("Connection error: " . mysqli_connect_error());
      }
    // selecting database
    mysqli_select_db($con, DB_DATABASE) or die(mysqli_connect_error());
    
    $in = join(',', array_fill(0, count($moduleids), '?'));
    $select = "SELECT * FROM modules WHERE id IN ($in)";
        $statement = $con->prepare($select);
        $statement->bind_param(str_repeat('i', count($moduleids)), ...$moduleids);
        $statement->execute();
    $result = $statement->get_result();
    
    $arr = array();
    while($row = mysqli_fetch_assoc($result)) { 
        $arr[] = $row;
    }

    mysqli_close($con);
    return $arr;
}

And the code outwith the function calling it looks like;

$id = $_POST['id'];
    
    $player = $db->getPlayerDetails($id);   
    if ($player != false) {
        $pid = $player["id"];
        $moduleids = $db->getModulesByPlayerId($pid);   //this one is okay
        $modules = $db->getTheseModulesById($moduleids); //problem here

        $response["player"]["id"] = $pid;           
        $response["player"]["fname"] = $player["fname"];
        $response["player"]["sname"] = $player["sname"];
        $response["modules"] = $modules;
        echo json_encode($response);

[EDIT] I should say, the moduleids are strings.

Community
  • 1
  • 1
DarkMalice
  • 205
  • 1
  • 11

0 Answers0