交互式爱情
您可以使用bindParam或bindValue方法来帮助准备您的陈述。它使事情看起来一目了然,而不是一目了然,$check->execute(array(':name' => $name));尤其是当您绑定多个值/变量时。检查以下清晰易读的示例:$q = $db->prepare("SELECT id FROM table WHERE forename = :forename and surname = :surname LIMIT 1");$q->bindValue(':forename', 'Joe');$q->bindValue(':surname', 'Bloggs');$q->execute();if ($q->rowCount() > 0){ $check = $q->fetch(PDO::FETCH_ASSOC); $row_id = $check['id']; // do something}如果您期望多行,请删除LIMIT 1并将fetch方法更改为fetchAll:$q = $db->prepare("SELECT id FROM table WHERE forename = :forename and surname = :surname");// removed limit 1$q->bindValue(':forename', 'Joe');$q->bindValue(':surname', 'Bloggs');$q->execute();if ($q->rowCount() > 0){ $check = $q->fetchAll(PDO::FETCH_ASSOC); //$check will now hold an array of returned rows. //let's say we need the second result, i.e. index of 1 $row_id = $check[1]['id']; // do something}