3

これは不適切なバインド (stackoverflow の検索) によるエラーであることがわかりましたが、すべての変数がバインドされており、タイプミスがないように見えます.... ここで明らかなことを見逃していますか?

function connect () {
global $pdo;
try {
    $pdo = new PDO("mysql:host=localhost;dbname=test", "root", "root");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
  }
}

function getPhotos ($status = 0, $type = null, $session = null, $name = null) {
global $pdo;
try {
    $sql = 'SELECT * FROM images WHERE 1=1';

    //Status Filter Query
    $sql .=' AND status = :status';

    //Type Filter Query
    if (!is_null($type)) {
        $sql .=' AND type = :type';
    }

    // Session Filter Query
    if (!is_null($session)) {
        $sql .=' AND session = :session';
    }

    // Twitter Handle Filter Query
    if (!is_null($name)) {
        $sql .=' AND handle = :name';
    }

    //Prepare The Query
    $stmt = $pdo->prepare($sql);

    //Fire The Lasers
    $stmt->execute(array(':status' => $status, ':type' => $type, ':session' => $session, ':name' => $name));

    //Grab 
    return $stmt->fetchAll();
4

1 に答える 1

8

Only bind the variables when you actually add them to the query:

//Status Filter Query
$sql .=' AND status = :status';

$vars = array(':status' => $status);

//Type Filter Query
if (!is_null($type)) {
    $sql .=' AND type = :type';
    $vars[':type'] = $type;
}

// Session Filter Query
if (!is_null($session)) {
    $sql .=' AND session = :session';
    $vars[':session'] = $session;
}

// etc.

$stmt->execute($vars);
于 2012-08-13T22:08:34.350 に答える