【问题标题】:php mysql delete statment does not runphp mysql删除语句不运行
【发布时间】:2016-06-27 19:18:43
【问题描述】:

我的 SQL 语句在 phpMyAdmin 中工作,但是当我使用 PHP 从我的网页运行它时,它什么也没做。

我的代码如下,它总是返回 true。这个问题我已经解决了,但是主要问题是代码没有删除行。

// Delete Area
public function deleteArea($product_area_id){
    $this->db->query("
    DELETE 
    FROM product_area 
    WHERE product_area_id = :product_area_id 
    LIMIT 1
    ");
    //bind
    $this->db->bind(':product_area_id', $product_area_id);
    //Execute
    if($this->db->execute()){
        return true;
    } else {
        return false;
    }
}

我的数据库类:

public function bind($param, $value, $type = null) {
    if (is_null ( $type )) {
        switch (true) {
            case is_int ( $value ) :
                $type = PDO::PARAM_INT;
                break;
            case is_bool ( $value ) :
                $type = PDO::PARAM_BOOL;
                break;
            case is_null ( $value ) :
                $type = PDO::PARAM_NULL;
                break;
            default :
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue ( $param, $value, $type );
}

public function query($query) {
    $this->stmt = $this->dbh->prepare($query);
}

public function execute(){
    return $this->stmt->execute();
}

【问题讨论】:

  • 你在这里混合 api
  • 对不起,我还在学习。混合api是什么意思。
  • 嘿@Fred-ii-,好久不见:D(但问题似乎还在继续)。对于操作,返回 truefalse 不是很有帮助,您应该查找一些对您有实际帮助的输出。
  • query 在我知道的每个 API 中都不适用于 bindexecute
  • 嘿@FirstOne 是的,已经有一段时间了。希望你一切都好。

标签: php mysql pdo delete-row


【解决方案1】:

您正在将 PDO 与 mysql_ 函数混合使用。它们不能互操作。这里需要的是rowCount

$this->db->query("
    DELETE 
    FROM product_area 
    WHERE product_area_id = :product_area_id 
    LIMIT 1
    ");
    //bind
    $this->db->bind(':product_area_id', $product_area_id);
    //Execute
    $this->db->execute();
    if(this->db->rowCount() > 0){

【讨论】:

  • 嗨 Machavity,现在它正确地意识到删除不起作用,但为什么删除语句不起作用。因为如果我将 SQL 粘贴到 phpmyadmin 中,请将 :product_area_id 更改为一个数字。它将删除该行。
  • 不应该是$this->statement->rowCount()吗?没有PDO::rowCount() 方法。
  • 抱歉给您带来了困惑。 db 是我添加的变量。所以 $this->db->rowCount() 和 $this->stmt->rowCount() 是一样的;
【解决方案2】:

来自操作的评论引用(目前的最后一条评论):

主要问题是,它不会删除。尽管 sql 直接在 phpmyadmin 中测试时,该语句是正确的。次要的 问题是,它也会说是真的。即使它没有删除。 Machavity 解决了次要问题。主要遗骸。 - 码农

TLDR: // 注意:PDOStatement::execute .... 成功时返回 TRUE,失败时返回 FALSE。

这就是为什么它总是为你返回 1。请参阅下面的 2 个文件,以使用 PDOStatement 对象的 rowCount() 进行更改来测试这一点。

您在 cmets 中说主要问题仍然存在。在手工制作数据库类之后,我使用下面的内容没有问题,因为你没有提供一个(你提到了一个)。注意 try/catch 块的使用。

坦率地说,我们不知道您是否有任何异常或您如何处理它们,或者您是否已激活错误报告。假设 pdo 对象可以成功返回 rowCount() 值,则以下内容应该能够通过任何测试。

用于测试的架构:

create table product_area
(   product_area_id int primary key,
    theName varchar(100) not null
);
--  blockA begin
truncate product_area;
insert product_area (product_area_id,theName) values
(1,'Houston zone B'),(2,'Houston zone BH'),(20,'Houston zone Z');
--  blockA end

测试文件:

对于测试文件,只有几行代码在靠近顶部的“迷你测试区”部分进行测试

<?php
//  pdo_del_test_20160703.php
// related to answering http://stackoverflow.com/questions/38061597 at the moment

error_reporting(E_ALL);
ini_set("display_errors", 1);
include "myPDO_DB.php"; // Database class. The class name = myPDO_DB

// Mini test area BEGIN:
$a1=new aClass();
$ret=$a1->deleteArea(2);
echo "retValue=".$ret."<br>";
// Mini test area ... END

class aClass {
    private $db=null;

    public function __construct(){
        echo "in constructor1<br>";
        $this->db=new myPDO_DB();
        echo "in constructor2<br>";
        //$this->db=null;
        echo "in constructor3<br>";
    }

    public function deleteArea($product_area_id){
        $this->db->query("
        DELETE  
        FROM product_area 
        WHERE product_area_id = :product_area_id  
        LIMIT 1
        ");
        // Note: PDOStatement::execute .... Returns TRUE on success or FALSE on failure.
        //
        // so on a Delete call, it just says sure, OK, done with that (if no exception)
        // It doesn't give feedback natively as to whether or not a row was actually deleted
        //
        //bind
        $this->db->bind(':product_area_id', $product_area_id);
        //Execute

        // Don't forget to run schema `blockA` before testing (so that data is there)
        if ($this->db->execute()) { // if this function returns anything other than TRUE you have exception problems
            $pdo_rowcount=$this->db->stmt->rowCount();  // see http://php.net/manual/en/pdostatement.rowcount.php

            // Depending on your systems ability to get pdo.rowCount() to work, consider the following

            // Please see http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_row-count
            $this->db->query("select row_count()"); // from mysql "Information Functions". Based on prior mysql call.
            $mysql_rowcount=$this->db->execute();

            // but note, row_count() in mysql is not exactly a solution for the majority of systems out there.
            // http://www.heidisql.com/forum.php?t=9791
            // on mysql 5.5 and my 5.6.24, it returns 0

            // so depending on your system, you will choose between $pdo_rowcount and $mysql_rowcount
            return $pdo_rowcount;   // your best bet, bust test it on your setup.
        }
        else {
            // do something
            return 0; // you would have already endured an exception though
        }
    }
}
?>

数据库类:

<?php
//  myPDO_DB.php
//
error_reporting(E_ALL);
ini_set("display_errors", 1);
include "db_connect_info.php";  // brings in the "defines" .. Shoot for a secure o/s vault in this file.

class myPDO_DB { 
    // will grab the stub from http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/
    //
    // and then build your class into it (because you did not provide it)
    //
    // and then further improve it with try/catch blocks that were lacking
    //

    private $host      = DB_HOST; // these were brought in with the include above. File not shown.
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;

    private $dbh;
    public $stmt;   // was made public to get into rowCount(); .... change this for your needs
    private $error;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // Set options
        $options = array(
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => false
        );

        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
            echo "Connect Ok<br>";
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    public function bind($param, $value, $type = null) {
        try {
            if (is_null ( $type )) {
                switch (true) {
                    case is_int ( $value ) :
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool ( $value ) :
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null ( $value ) :
                    $type = PDO::PARAM_NULL;
                    break;
                default :
                    $type = PDO::PARAM_STR;
                }
            }
            $this->stmt->bindValue ( $param, $value, $type );
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
            throw $e;
        }
    }

    public function query($query) {
        try {
            $this->stmt = $this->dbh->prepare($query);
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
            throw $e;
        }
    }

    public function execute(){
        try {
            return $this->stmt->execute();
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
            throw $e;
        }
    }
} 

【讨论】:

  • 另请注意,如果您要删除的 id 是主键(我只是将一些东西存根在一起),则不需要您的 LIMIT 1
【解决方案3】:

对不起,我失礼了。我传入的值是 $Product_area_id,而我使用的是 $product_area_id。上帝。

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2013-07-27
    • 1970-01-01
    • 2011-09-01
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多