Php mysql in array query not working using pdo

$query = "SELECT admin_name FROM admin WHERE admin_code IN('123','6895')";
    try
    {
        $result = $this->dbh->prepare($query);
        $result->execute($parameter);
        $data = $result->fetch(\PDO::FETCH_ASSOC);
        return $data;
    }
    catch (\PDOException $e)
    {
        return false;
    }

what is the error of this code i can’t able to get return value

i run the same query in mysql working fine.

what is the error-code/exception you get from the parser? use $pdo->errorInfo()

no error message found
catch (\PDOException $e)
{
print_r($e->getmessage());die();
}

It might be falling over at that line, the \ in that line is probably tripping it up

So,what is the error of this code

If you have set PDO into exception mode, just log the exception message.

It probably bails out on the incorrect parameter number.

Is the code you posted your actual code? I can’t help but think you are trying to pass an array of values to the IN clause via $parameters. Which is simply not going to work out of the box as prepared statements (sadly) do not support array values.

And yet, when I was trying that code on my local server to try to see what might be wrong, I first created $parameters as a blank string and got the error that it must be an array. I don’t normally use PDO in this way, I prefer to use bindParam(), so I am by no means sure, but I thought it was acceptable to use an indexed array to pass the parameters into a PDO prepared statement?

public bool PDOStatement::execute ([ array $input_parameters ] )

In any case, as the query has no parameters it probably won’t matter. The code above seemed to work OK for me.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.