php中的multi_query

时间:2010-08-20 09:44:52

标签: php mysqli phpmyadmin

我有一个带有两个表的数据库'test'。

这是phpmyadmin转储:

CREATE TABLE IF NOT EXISTS `tags` (
  `name` varchar(100) NOT NULL,
  `id` int(4) NOT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tags`
--

INSERT INTO `tags` (`name`, `id`) VALUES
('tag_one', 1),
('tag_two', 1),
('tag_three', 1);

-- --------------------------------------------------------

--
-- Table structure for table `tags_used`
--

CREATE TABLE IF NOT EXISTS `tags_used` (
  `name` varchar(100) NOT NULL,
  `used` int(4) NOT NULL,
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tags_used`
--

INSERT INTO `tags_used` (`name`, `used`) VALUES
('tag_one', 10),
('tag_three', 10),
('tag_two', 10);

这是php代码:

<?php

$mysqli = new mysqli('127.0.0.1', 'root', 'pass', 'test');
if ($mysqli->connect_error) {
    echo 'Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
}
else {
    $query = <<<SQL
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_one' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_one';
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_two' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_two';
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_three' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_three';
SQL;

    $query = trim($query);
    $mysqli->multi_query($query);
    $mysqli->close();
}

?>

执行后'tag_three'仍然在tags表中 当我通过phpmyadmin运行这个SQL时,它工作正常 我哪里错了?


mysql 5.1.42
php 5.3.1
windows 2003
nginx 0.8.32


MySQL.log

C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld, Version: 5.1.42-community-log (MySQL Community Server (GPL)). started with:
TCP Port: 3306, Named Pipe: (null)
Time                 Id Command    Argument
100821 12:25:52     1 Connect   root@localhost on test
            1 Query DELETE FROM `tags` WHERE `tags`.`name` = 'tag_one' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_one';
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_two' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_two';
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_three' AND `tags`.`id` = '1';
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_three'
100821 12:27:37     2 Connect   root@localhost on 
            2 Query SET CHARACTER SET 'utf8'
            2 Query SET collation_connection = 'utf8_general_ci'
            2 Query SHOW PLUGINS
            2 Init DB   test
            2 Init DB   test
            2 Query SELECT * FROM `tags`
 LIMIT 0, 30
            2 Query SHOW TABLE STATUS FROM `test` LIKE 'tags%'
            2 Query SELECT COUNT(*) FROM `test`.`tags`
            2 Init DB   test
            2 Init DB   test
            2 Query SHOW VARIABLES LIKE 'profiling'
            2 Query SHOW FULL COLUMNS
        FROM `test`.`tags`
            2 Quit  

1连接 - 这是我的代码
2 Connect - 来自phpmyadmin的“刷新”


php日志中没有错误。


它符合代码。原样。此代码之前或之后没有任何疑问。

1 个答案:

答案 0 :(得分:0)

您应该检查错误日志,并在此处发布部分内容?因为它似乎是多查询功能的怪癖。我已经尝试了一个大致相同配置的代码(除了操作系统,win服务器2008),它的工作原理。我也尝试将其修改为六个不同的查询,它也有效。

我已尝试将查询更改为以下内容:

  $query = <<<SQL
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_one' AND `tags`.`id` = '1';
COMMIT;
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_one';
COMMIT;
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_two' AND `tags`.`id` = '1';
COMMIT;
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_two';
COMMIT;
DELETE FROM `tags` WHERE `tags`.`name` = 'tag_three' AND `tags`.`id` = '1';
COMMIT;
UPDATE `tags_used` SET `used` = used-1 WHERE `tags_used`.`name` = 'tag_three';
COMMIT;
SQL;

希望它适合你