mysql使用本地db项目中的模式在php中创建新数据库时创建db结构

时间:2014-12-11 04:58:28

标签: php mysql pdo

我一直在我的localhost上创建一个项目,我想使用我的本地应用程序在我的实际站点上使用的数据库结构。我已经完成了一个mysql转储,并尝试将其添加到我的pdo数据库创建脚本(在实际站点上),但是当创建数据库时,它们不是结构。我不知道我做错了什么,我很难找到它的任何例子。我不太清楚我错过了什么指针会很棒。提前致谢

代码ex。

$host="localhost"; 
$root="root"; 
$root_password="root"; 
$user='newuser';

$pass='newpass';
$db= 'companyid' . $companyid; 
    try {
        $dbh = new PDO("mysql:host=$host", $root, $root_password);

        $dbh->exec("CREATE DATABASE `$db`;
                CREATE USER '$user'@'localhost' IDENTIFIED BY '$pass';
                GRANT ALL ON `$db`.* TO '$user'@'localhost';
                FLUSH PRIVILEGES;
                CREATE TABLE IF NOT EXISTS `address` (
  `address` varchar(50) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

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

--
-- Table structure for table `checkpics`
--

CREATE TABLE IF NOT EXISTS `checkpics` (
  `image` varchar(120) COLLATE latin1_general_ci NOT NULL,
  `amount` decimal(10,0) NOT NULL,
  `date` date NOT NULL,
  `photosmall` varchar(64) COLLATE latin1_general_ci NOT NULL,
  `photobig` longblob NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `customerid` int(10) NOT NULL,
  `checknumber` int(10) NOT NULL,
  `checkamount` varchar(10) COLLATE latin1_general_ci NOT NULL,
  `datepaid` varchar(10) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

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

--
-- Table structure for table `CityStateZip`
--

CREATE TABLE IF NOT EXISTS `CityStateZip` (
  `city` varchar(30) COLLATE latin1_general_ci NOT NULL,
  `state` varchar(2) COLLATE latin1_general_ci NOT NULL,
  `zip` varchar(10) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;") 
        or die(print_r($dbh->errorInfo(), true));

    } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
    }

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。创建多个表时,必须运行单独的命令或使用mysqli多查询命令,并通过;

分隔每个实例