Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用mysqli::query和mysqli::execute不会返回错误,但不会在数据库中执行_Php_Mysql_Mysqli_Mysqli Multi Query - Fatal编程技术网

Php 调用mysqli::query和mysqli::execute不会返回错误,但不会在数据库中执行

Php 调用mysqli::query和mysqli::execute不会返回错误,但不会在数据库中执行,php,mysql,mysqli,mysqli-multi-query,Php,Mysql,Mysqli,Mysqli Multi Query,我正在尝试执行DROP临时表语句。我使用MySQL Workbench运行了该语句,它在那里工作。我已经确保在PHP和MySQL Workbench中以相同的用户身份成功登录 我已经尝试使用mysqli::query和mysqli::prepare-then-mysqli::execute方法。当我尝试执行DROP TEMPORATE TABLE语句时,这两种方法都不起作用。但是,当我执行INSERT或SELECT语句时,这两种方法都可以工作 此代码不会返回错误,但也不会在数据库中执行: pub

我正在尝试执行DROP临时表语句。我使用MySQL Workbench运行了该语句,它在那里工作。我已经确保在PHP和MySQL Workbench中以相同的用户身份成功登录

我已经尝试使用mysqli::query和mysqli::prepare-then-mysqli::execute方法。当我尝试执行DROP TEMPORATE TABLE语句时,这两种方法都不起作用。但是,当我执行INSERT或SELECT语句时,这两种方法都可以工作

此代码不会返回错误,但也不会在数据库中执行:

public function executeSQL()
{       
    // SQL statement using query method
    if (!$this->mySQLi->query("DROP TEMPORARY TABLE IF EXISTS TIME_INTERVAL_DATA") ) 
    {
        $this->writeSQLErrorToLog("Query method returned an error.");
    }
    if(!$this->mySQLi->commit()) 
    {
        // Committing the transaction failed.
        $this->writeSQLErrorToLog("Commit method returned an error.");
    }

    // SQL statement using prepare and execute methods
    $stmt = $this->mySQLi->prepare("DROP TEMPORARY TABLE IF EXISTS TIME_INTERVAL_DATA");
    if($stmt) 
    {
        if(!$stmt->execute())
        {
            // Execute method returned an error.
            $this->writeSQLErrorToLog("Execute method returned an error.");
            $this->throwMySQLErrorAndCloseStatement($stmt);
        }
    } else {
        // Prepare method returned an error.
        $this->writeSQLErrorToLog("Prepare method returned an error.");
        return;
    }
    // ...
}
$stmt = $this->mySQLi->prepare("INSERT INTO TABLE1 (ID) VALUES (1)");
if(!$stmt->execute()) {
    // Return the MySQL Error message and number
    $this->throwMySQLErrorAndCloseStatement($stmt);
}
但是,以下代码确实在数据库中执行:

public function executeSQL()
{       
    // SQL statement using query method
    if (!$this->mySQLi->query("DROP TEMPORARY TABLE IF EXISTS TIME_INTERVAL_DATA") ) 
    {
        $this->writeSQLErrorToLog("Query method returned an error.");
    }
    if(!$this->mySQLi->commit()) 
    {
        // Committing the transaction failed.
        $this->writeSQLErrorToLog("Commit method returned an error.");
    }

    // SQL statement using prepare and execute methods
    $stmt = $this->mySQLi->prepare("DROP TEMPORARY TABLE IF EXISTS TIME_INTERVAL_DATA");
    if($stmt) 
    {
        if(!$stmt->execute())
        {
            // Execute method returned an error.
            $this->writeSQLErrorToLog("Execute method returned an error.");
            $this->throwMySQLErrorAndCloseStatement($stmt);
        }
    } else {
        // Prepare method returned an error.
        $this->writeSQLErrorToLog("Prepare method returned an error.");
        return;
    }
    // ...
}
$stmt = $this->mySQLi->prepare("INSERT INTO TABLE1 (ID) VALUES (1)");
if(!$stmt->execute()) {
    // Return the MySQL Error message and number
    $this->throwMySQLErrorAndCloseStatement($stmt);
}
我已经阅读了尽可能多的MySQL和PHP文档。我已经阅读并重新阅读了上面所有mysqli方法的文档。我已经读过并尝试过使用mysqli::escape_字符串的各种变体,但当我使用它时,会返回一个错误。我还尝试了许多不同SQL语句的变体


简而言之,所有INSERT和SELECT语句都可以工作。但是,DROP TEMPORARY TABLE语句永远不会工作,也不会返回错误。这件事真让我抓狂。任何帮助都将不胜感激

它可能不存在于该会话中。临时表的作用域仅限于您的会话,因此,如果您断开连接并重新连接,它仍然会消失。您是否在另一个请求中使用相同的连接检查表是否存在?事实上,它们是暂时的吗?throwMySQLErrorAndCloseStatement.:O这是一个很好的函数名。谢谢Wrikken,你说得对!!我实际上试着执行3条语句。drop temp表只是第一个。我没有意识到临时表在会话范围内是有限的。在你发表评论之后,我把另外两条SQL语句放回去,它现在就可以工作了。非常感谢!!!如果你想发布一个答案,我会把它标记为正确的,这样你就可以得到它的分数。而yah Devon,我是一个iOS的家伙,所以我习惯了详细的函数名。我想这对我来说是一种困扰。lol:-