2017-01-22 60 views
-2
$selected_id = -1; 
$selected_name = ""; 
if(isset($_GET['cid'])){ 
    $cid = $_GET['cid']; 
    $res = $pdo->prepare("SELECT * FROM k WHERE id = :cid"); 
    $res->bindParam(":cid",$cid); 
    $result = $res->execute(); 

    $rw = $result->fetchAll([$cid]); 
    if($rw){ 
     $selected_id = $rw['id']; 
     $selected_name = $rw['ime']; 
    } 
} 

調用一個成員函數使用fetchall()我想只選擇ID,並與已選定的ID進行比較,但總是這個錯誤是任何人可以幫助我,請...致命錯誤:布爾

回答

1

$res->execute返回一個布爾值,你在$result變量保存:

http://php.net/manual/en/pdostatement.execute.php

您沒有使用相同的對象,所以你不能使用對象的fetchAll方法。你應該使用相同的對象,所以你應該這樣做,而不是:

$selected_id = -1; 
$selected_name = ""; 
if(isset($_GET['cid'])){ 
    $cid = $_GET['cid']; 
    $res = $pdo->prepare("SELECT * FROM k WHERE id = :cid"); 
    $res->bindParam(":cid",$cid); 
    $res->execute(); // Run the method 

    $rw = $res->fetchAll([$cid]); // Store the fetchAll result in a new variable 
    if($rw){ 
     $selected_id = $rw['id']; 
     $selected_name = $rw['ime']; 
    } 
}