0

如何将其作为 PDO 查询运行?

$query = "SELECT * FROM `Top_10` WHERE DOB =". $date;
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)==0){
while blah blah
}

我可以运行 PDO 提取数组。但是一旦我这样做,它就会失败。

有任何想法吗?

4

1 回答 1

0
$dbh = new PDO("mysql:dbname=$dbname", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

try {
  $qry = $dbh->prepare('SELECT * FROM Top_10 WHERE DOB = ?');
  $qry->execute([$date]);

  while ($row = $qry->fetch()) {
    // blah blah blah
  }
}
catch (PDOException $e) {
  die($e->getMessage());
}
于 2013-07-10T16:12:37.797 回答