Funguje mi:
$pdo = new PDO('mysql:dbname=databaze;host=localhost', 'jmeno', 'heslo');
$dotaz = $pdo->prepare("SELECT 1 FROM tabulka WHERE id LIKE CONCAT('%',?,'%')");
$dotaz->execute(array($_GET["id"]));
$existuje = $dotaz->fetchColumn();
header("Content-type: image/png");
$im = imagecreatefrompng($existuje?"ano.png":"ne.png");
imagepng($im);
imagedestroy($im);