PHP行执行(MySQL命令)两次(或者一旦注释掉就不执行)

时间:2016-02-26 05:08:41

标签: php mysql sql

我有一个数据库升级脚本,它接受当前数据库版本,通过一个巨大的switch语句传递没有任何中断所以所有后续查询都运行,然后将数据库号更新为最新版本。

免责声明:我知道可能有更好的方法(比如一堆if($version>1507)支票,所以我不必为#34;空"版本提供案例但我没有时间进行重构和测试。我最终会,但我真的不认为这个问题是相关的。

以下是升级脚本的示例,包括给我提出问题的行:

case 1508:
    addNotification("Updating 1508: Adding feature specific tables for wake-up calls, virtual dispatcher and screen pop, to allow better usage and billing tracking", "success");
    dbQuery("CREATE TABLE IF NOT EXISTS `feature_wakeup_calls` ( `feature_wakeup_call_id` int(12) NOT NULL AUTO_INCREMENT, `employee_id` int(12) NOT NULL, `date` date NOT NULL, `confirmed` datetime DEFAULT NULL, `employee_sms_id` int(12) DEFAULT NULL, `employee_call_id` int(12) DEFAULT NULL, `authority_call_id` int(12) DEFAULT NULL COMMENT 'call record to the authority', `balance_id` int(12) NOT NULL COMMENT 'references balance table', PRIMARY KEY (`feature_wakeup_call_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
    dbQuery("CREATE TABLE IF NOT EXISTS `feature_virtual_dispatcher` ( `feature_virtual_dispatcher_id` int(12) NOT NULL AUTO_INCREMENT, `inbound_call_id` int(12) NOT NULL, `outbound_call_id` int(12) DEFAULT NULL, `balance_id` int(12) NOT NULL, PRIMARY KEY (`feature_virtual_dispatcher_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
    dbQuery("CREATE TABLE IF NOT EXISTS `feature_screen_pop_endpoints` ( `feature_screen_pop_endpoint_id` int(12) NOT NULL AUTO_INCREMENT, `mac_address` varchar(50) NOT NULL, `created_by` int(12) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`feature_screen_pop_endpoint_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
    dbQuery("CREATE TABLE IF NOT EXISTS `feature_screen_pop` ( `feature_screen_pop_id` int(12) NOT NULL AUTO_INCREMENT, `mac` varchar(200) DEFAULT NULL, `phone_number` int(12) NOT NULL, `format` varchar(200) DEFAULT NULL COMMENT 'ex: xml, html, etc', `when` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`feature_screen_pop_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
case 1509:
case 1510:
case 1511:
    addNotification("Updating 1511: New columns to the phone companion endpoints table", "success");
    dbQuery("ALTER TABLE `feature_screen_pop_endpoints` ADD `name` VARCHAR(200) NULL AFTER `feature_screen_pop_endpoint_id`;");
    dbQuery("ALTER TABLE `feature_screen_pop_endpoints` ADD `extension` VARCHAR(50) NULL DEFAULT NULL AFTER `mac_address`, ADD `manufacturer` ENUM('Polycom','Astra','Mitel/YeaLink','Grandstream') NULL DEFAULT NULL AFTER `extension`, ADD `model` VARCHAR(50) NULL DEFAULT NULL AFTER `manufacturer`;");

我遇到的问题是1511之后的dbQuery电话失败。如果我发表评论,那么下一个就会失败。这是dbQuery函数:

function dbQuery($query, $returnId = false, $connection = null) {
    $close = true; // by default, close the connection once done
    if($connection == null) $connection = connect();
    else $close = false; // don't close the connection if it was passed to us
    if($returnId == false) {
        $result = mysqli_query($connection, $query);
        if($result == false && !stringContains($query, "notification_log")) { // prevent infinite loops
            addNotification("Query Failed: ".$query." with error: ".mysqli_error($connection));
        }
        if($close) mysqli_close($connection);
        return $result;
    }
    else {
        $result = mysqli_query($connection, $query);
        if($result == false) {
            if($close) mysqli_close($connection);
            return $result;
        }
        else {
            $result = mysqli_insert_id($connection);
            if($close) mysqli_close($connection);
            return $result;
        }
    }
}

注意Query Failed部分。我的日志说:

Query Failed: ALTER TABLE `feature_screen_pop_endpoints` ADD `name` VARCHAR(200) NULL AFTER `feature_screen_pop_endpoint_id` with error: Duplicate column name 'name'

如果我只注释掉那一行并完全开始(新数据库和所有内容),我在下一行会收到类似的错误:

Query Failed: ALTER TABLE `feature_screen_pop_endpoints` ADD `extension` VARCHAR(50) NULL DEFAULT NULL AFTER `mac_address`, ADD `manufacturer` ENUM('Polycom','Astra','Mitel/YeaLink','Grandstream') NULL DEFAULT NULL AFTER `extension`, ADD `model` VARCHAR(50) NULL DEFAULT NULL AFTER `manufacturer` with error: Duplicate column name 'extension'

当然,我检查了是否有其他地方创建了这些表(他们没有,如果我在1508中注释掉他们从未存在过的行),并且有类似的dbQuery调用正在执行ALTER TABLE table_name ADD运作完美无缺。例如,上面只有几行:

case 1502:
    addNotification("Updating 1502: More data for the balance table", "success");
    dbQuery("ALTER TABLE `balance` ADD `extra` VARCHAR(500) NULL AFTER `method`, ADD `unique` VARCHAR(200) NULL AFTER `extra`;");

我已经反复测试过,所以我知道这是这些行(1511集),但我之前遇到过这个问题,我不得不重新基线化整个数据库,绕过开关中的第一~1300例。虽然这可以在这里起作用,但它正在治疗一种症状并且不会扩大。

任何人都有任何关于可能出错的线索?

1 个答案:

答案 0 :(得分:-2)

我遇到了同样的问题。  enter image description here

我发现浏览器总是在我的php脚本之后请求'favicon.ico'。它的请求网址为{my_domain}/favicon.ico

所以显然'favicon.ico'请求会导致再次运行脚本。