如何检查准备好的语句是否有结果

发布于 2024-09-24 02:48:31 字数 614 浏览 5 评论 0原文

过去我会这样做:

  $sql = 'SELECT * FROM customers WHERE customer_email="' . mysql_real_escape_string($_POST['customer_email']) . '" ';
  $res = mysql_query($sql);

  // if there are no hits...
  if(mysql_num_rows($res) == FALSE) {

今天我正在做同样的事情,但是使用准备好的语句:

  $stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
  if ($stmt->execute(array($_POST['customer_email']))) {

我准备好的语句的第二行 if($stmt... 是“如果这个查询得到结果”还是“如果无论结果如何都执行此查询,即如果它执行时没有错误”。

我想要解决的是使用准备好的语句如何执行相当于 mysql_num_rows() == FALSE 的操作?

谢谢!!

In the past I would do something like so:

  $sql = 'SELECT * FROM customers WHERE customer_email="' . mysql_real_escape_string($_POST['customer_email']) . '" ';
  $res = mysql_query($sql);

  // if there are no hits...
  if(mysql_num_rows($res) == FALSE) {

Today I am doing the same thing however with prepared statements:

  $stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
  if ($stmt->execute(array($_POST['customer_email']))) {

The 2nd line of my prepared statement if($stmt... is that "if this query gets a result" or is it "if this query is executed regardless of results or not ie if it executes without error".

What I'm trying to work out is with prepared statements how do you do the equivalent of mysql_num_rows() == FALSE?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-10-01 02:48:31

您可以使用 PDOStatementrowCount() 方法来获取返回的行数:

$stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
$stmt->execute(array($_POST['customer_email']));
if($stmt->rowCount() > 0) {
   //fetch stuff...
}

或者,如果 rowCount() 被证明不可靠,你可以这样做:

$all = $stmt->fetchAll();
if(count($all)) {
   //loop through the set...
}

You can use the rowCount() method of PDOStatement to get the number of rows returned:

$stmt = $dbh->prepare("SELECT * FROM customers where customer_email = ? LIMIT 1");
$stmt->execute(array($_POST['customer_email']));
if($stmt->rowCount() > 0) {
   //fetch stuff...
}

Or, if rowCount() proves to be unreliable, you can do this:

$all = $stmt->fetchAll();
if(count($all)) {
   //loop through the set...
}
烟雨凡馨 2024-10-01 02:48:31

PDOStatement::rowCount() 返回受 DELETE、INSERT 或 UPDATE 语句影响的行数,而不是 select 查询返回的行数,
对于选择查询我使用:
fetchAll(PDO::FETCH_OBJ);

回声计数($lignes);

<?php
$PARAM_hote='localhost'; 
$PARAM_port='3306';
$PARAM_db='test'; 
$PARAM_user='root'; 
$PARAM_pwd=''; 
try
{
$connexion = new PDO('mysql:host='.$PARAM_hote.';dbname='.$PARAM_db, $PARAM_user,$PARAM_pwd);
}
catch(Exception $e)
{
echo 'Erreur : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
$requete_prepare_1=$connexion->prepare("SELECT * FROM `test_mysql`"); 
$requete_prepare_1->execute();
$lignes=$requete_prepare_1->fetchAll(PDO::FETCH_OBJ);
echo count($lignes); 
?>

PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement and not the number of rows returned by select query,
for select query I use :
fetchAll(PDO::FETCH_OBJ);
And
echo count($lignes);

<?php
$PARAM_hote='localhost'; 
$PARAM_port='3306';
$PARAM_db='test'; 
$PARAM_user='root'; 
$PARAM_pwd=''; 
try
{
$connexion = new PDO('mysql:host='.$PARAM_hote.';dbname='.$PARAM_db, $PARAM_user,$PARAM_pwd);
}
catch(Exception $e)
{
echo 'Erreur : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
$requete_prepare_1=$connexion->prepare("SELECT * FROM `test_mysql`"); 
$requete_prepare_1->execute();
$lignes=$requete_prepare_1->fetchAll(PDO::FETCH_OBJ);
echo count($lignes); 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文