2011-08-10 24 views
1

我對準備好的語句很新,我目前正在通過我所有的代碼來更新它。在代碼塊中綁定var的PHP mysqli幫助?

我需要一點幫助重寫下面的代碼:

 if($stmt = $db->query("select * from product where active=1 and id=?")){ 
      echo "Returned Result"; 
     }else{ 
      echo "Invalid SQL"; 
     } 

使用這個代碼,我需要綁定變量$ _ POST [「身份證」]:

  $stmt->bind_param("s", $_POST['id']); 

在那裏我會放綁定獲取整個代碼塊的工作?

在此先感謝

回答

1

相反的query()你需要調用prepare()

// Prepare the statement first and bind params 
$stmt = $db->prepare("select * from product where active=1 and id=?")){ 
$stmt->bind_param("s", $_POST['id']); 

// Then execute it 
if ($stmt->execute()) { 
    echo "Returned Result"; 
    // Then fetch your results 
} else { 
    echo "Query failed"; 
}