运行PDO mysql_num_rows查询

时间:2013-07-10 16:03:52

标签: mysql pdo mysql-num-rows

如何将其作为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 fetch数组。但是一旦我这样做,就会失败。

有什么想法吗?

1 个答案:

答案 0 :(得分: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());
}