mysql查询创建表不能通过php工作

时间:2016-01-18 17:36:56

标签: php mysql

我试图将2个表结构复制到新数据库中。 我使用一个自己创建SQL命令的函数:当我在phpmyadmin上执行代码时代码被执行但是当我通过PHP执行它时,它不会执行。

怎么可能?

sql命令是这样的:

CREATE TABLE `tabella_1` ( 
  `campo1` int(11) NOT NULL AUTO_INCREMENT,  
  `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,  
  `campo_3` int(11) NOT NULL,  
  PRIMARY KEY (`campo1`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE `tabella_2` (  
  `campo1` int(11) NOT NULL DEFAULT '0',  
  `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,  
  `campo_3` int(11) NOT NULL,  
  PRIMARY KEY (`campo1`)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

由于

2 个答案:

答案 0 :(得分:0)

使用您的数据库连接详细信息更新此代码:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE `tabella_1` ( `campo1` int(11) NOT NULL AUTO_INCREMENT, `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";

if ($conn->query($sql) === TRUE) {
    echo "Table 1 created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

// sql to create table
$sql2 = "CREATE TABLE `tabella_2` ( `campo1` int(11) NOT NULL DEFAULT '0', `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";

if ($conn->query($sql2) === TRUE) {
    echo "Table 2 created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?> 

答案 1 :(得分:0)

我明白了!

我在数组中放入了函数,并且它有效!

Tkanks to everybody!