无法创建我的表

时间:2014-03-24 23:55:25

标签: php mysql create-table

我已尝试使用/不使用引号,使用/不使用or exit(),我检查我已正确连接到数据库。

修改:现在我收到了自己的自定义错误:

$username="user";
$password="*****";
$hostname="localhost";

mysql_connect($hostname,$username,$password) or exit("Error: @mysql_connect.");
mysql_select_db("test") or exit("Error: @mysql_select_db.");

mysql_query("CREATE TABLE propiedades(
'id' SMALLINT NOT NULL AUTO_INCREMENT,
'colonia' VARCHAR(35),
'zona' VARCHAR(35),
'precio' DECIMAL(9,2),
'usd' BOOLEAN,
'tipo' TINYINT,
'venta' BOOLEAN,
'recamaras' TINYINT,
'banos' TINYINT,
'mbanos' TINYINT,
'plantas' TINYINT,
'construccion' SMALLINT,
'terreno' SMALLINT,
'edad' TINYINT,
'uploaded' TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
'updated' TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
'ocultar' BOOLEAN NOT NULL DEFAULT 0,
PRIMARY KEY (id))") or exit("Error: @mysql_query CREATE TABLE.");

我的代码正在抛出"Error: @mysql_query CREATE TABLE."

2 个答案:

答案 0 :(得分:-1)

好吧,您应该将整个SQL代码放入 mysql_query 函数

作为论据:

$result = mysql_query('CREATE TABLE propiedades(
    "id" SMALLINT NOT NULL AUTO_INCREMENT,
    "colonia" VARCHAR(35),
    "zona" VARCHAR(35),
    "precio" DECIMAL(9,2),
    "usd" BOOLEAN,
    "tipo" TINYINT,
    "venta" BOOLEAN,
    "recamaras" TINYINT,
    "banos" TINYINT,
    "mbanos" TINYINT,
    "plantas" TINYINT,
    "construccion" SMALLINT,
    "terreno" SMALLINT,
    "edad" TINYINT,
    "uploaded" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "updated" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "ocultar" BOOLEAN NOT NULL DEFAULT 0,
    PRIMARY KEY (id))');

答案 1 :(得分:-1)

我没有检查你的查询的语法,我只是在演示它应该如何完成。正如马里奥在上面的评论中所说的那样,你不能将纯粹的mysql代码提供给php并期望它能够工作......这样做你需要使用mysql api或pdo。

$sql = '
    CREATE TABLE propiedades(
    `id` SMALLINT NOT NULL AUTO_INCREMENT,
    `colonia` VARCHAR(35),
    `zona` VARCHAR(35),
    `precio` DECIMAL(9,2),
    `usd` BOOLEAN,
    `tipo` TINYINT,
    `venta` BOOLEAN,
    `recamaras` TINYINT,
    `banos` TINYINT,
    `mbanos` TINYINT,
    `plantas` TINYINT,
    `construccion` SMALLINT,
    `terreno` SMALLINT,
    `edad` TINYINT,
    `uploaded` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `ocultar` BOOLEAN NOT NULL DEFAULT 0,
    PRIMARY KEY (id))';

$result = mysql_query($sql);

if (!$result)
    exit("Error: @CREATE TABLE.");