【问题标题】:How can I make sure that DELETE SQL statement in Postgres using PHP was successfull?如何确保使用 PHP 在 Postgres 中的 DELETE SQL 语句成功?
【发布时间】:2012-07-31 10:28:18
【问题描述】:

是否有一个 函数 会根据 DELETE SQL 语句是否成功返回 true 或 false?例如这样的:

<?php
    $sql = "DELETE FROM table WHERE id=123";
    $result = pg_query($sql);

    if **function**($result)
        return true;
    else
        return false;
?>

此外,是否有一个函数可以返回成功删除的行数?

【问题讨论】:

    标签: php postgresql


    【解决方案1】:

    使用mysql_affected_rows() 获取mysql 中受影响的行数。

    与 postgres 类似,它将是 pg_affected_rows

    【讨论】:

    • 我正在使用 postgres,但是由于现在我发现 postgres pg_affected_rows 存在基本相同的功能
    • hmmm,我想我在标题和标签中已经说得很清楚了;),谢谢你让我搜索并找到我需要的功能
    【解决方案2】:

    if() 是函数 ;)

    if($result)
    return true;
    else
    return false;
    

    如果你想知道成功删除的行数,循环 if 条件

    $flag=0;
    if($result)
    {
       $flag++;
    }
    if($flag==0;)
    echo "Nothing is deleted";
    else
    echo $flag." rows are deleted";
    

    【讨论】:

      【解决方案3】:
      if($result)
      {
        // Delete was successful
      }
      else
      {
        // was not successful
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        相关资源
        最近更新 更多