我有以下文件schema.sql
用于MySQL数据库:
--
-- schema.sql
--
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `sometable` (`blah` tinyint(1) not null) Engine=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
DELIMITER //
CREATE PROCEDURE call_procedure()
BEGIN
SET @column_exists = (SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = (SELECT DATABASE())
AND TABLE_NAME = 'sometable'
AND COLUMN_NAME = 'stuff');
-- Only modify if column exists
IF @column_exists IS NULL THEN
ALTER TABLE sometable ADD COLUMN stuff TINYINT(1) NOT NULL;
END IF;
END;
//
DELIMITER ;
-- Execute the procedure
CALL call_procedure();
-- Drop the procedure
DROP PROCEDURE call_procedure;
当我在Ubuntu上转到bash命令行时,我可以运行此命令并确切获得所需的结果:
mysql -u root -pMyPass mydatabase < schema.sql
换句话说,表sometable
已创建,我看到两列blah
和stuff
。
因此,我创建了一个如下的PHP脚本:
/// schema.php
$schema = file_get_contents('schema.sql');
$PDO = new PDO("mysql:host=localhost;dbname=mydatabase","root","MyPass");
$PDO->exec("set names utf8");
$PDO->exec($schema);
运行此脚本时,我得到一个只有一列sometable
的数据库表blah
。列stuff
从未创建。
为什么PHP不运行bash命令行的方式运行我的sql脚本并创建列stuff
?