prepare($ sql) - > execute()有效,但$ statement-> execute()没有

时间:2018-01-27 13:07:08

标签: php mysql pdo prepared-statement execute

如果我使用以下语句,我的代码运行良好:

$statement = $this->pdo->prepare($sql)->execute();

但如果我使用以下语句,我的代码就不起作用了:

$statement = $this->pdo->prepare($sql);
$statement->execute();

有没有人有想法,我做错了什么或为什么会这样?

这是我的完整代码:

    public function deleteUser($pid_user){
    /* DESCRIPTION
     * delete an user an all his data
     *  
     * PARAMETERS
     * 
     * EXAMPLE
     * deleteUser();
     */
    try {

        //begin transaction
        $this->pdo->beginTransaction();

        //define all tables to delete all entries from the overgiven user id
        //name = name of the table
        //column = column to identify the users entries
        $tables = array();
        $tables[0]["name"]      = "snsho_bittrex_apikey";
        $tables[0]["column"]    = "fk_user";
        $tables[1]["name"]      = "snsho_bittrex_balances";
        $tables[1]["column"]    = "fk_user";
        $tables[2]["name"]      = "snsho_bittrex_deposit_history";
        $tables[2]["column"]    = "fk_user";
        $tables[3]["name"]      = "snsho_bittrex_order_history";
        $tables[3]["column"]    = "fk_user";
        $tables[4]["name"]      = "snsho_bittrex_withdrawal_history";
        $tables[4]["column"]    = "fk_user";
        $tables[5]["name"]      = "snsho_user_settings";
        $tables[5]["column"]    = "fk_user";
        $tables[6]["name"]      = "snsho_user";
        $tables[6]["column"]    = "pid_user";


        //do the queries
        $sql = '';
        foreach($tables as $key => $table){
            $sql .= 'DELETE FROM ' . $table["name"] . ' WHERE ' . $table["column"] . ' = ' . $pid_user . ';';
        }

        //$statement = $this->pdo->prepare($sql)->execute();
        $statement = $this->pdo->prepare($sql);
        $statement->execute();

        if($this->pdo->commit()){
            echo "commited";
        }else{
            echo "commit failed";
        }           
        return TRUE;
    } catch (Exception $e) {
        $this->adminMessages->setSingleError("Failed: " . $e->getMessage());
        $this->pdo->rollBack();
        return FALSE;
    }   
}

1 个答案:

答案 0 :(得分:0)

尝试不带作业的执行。

$this->pdo->prepare($sql)->execute();

这只返回true或false。