使用PHP在MySql中创建动态表

时间:2014-05-31 06:05:38

标签: php mysql

我试图在php中动态创建mysql中的表

$usernametable="test".$usname;
    $create = ("CREATE TABLE $usernametable(
    id SMALLINT NOT NULL PRIMARY KEY,
    test1 SMALLINT, test2 SMALLINT, test3 SMALLINT )");


    $createtable = mysql_query($create, $conn) 
    or die ('Problem with query' . mysql_error());  

它似乎没有工作,我甚至没有收到错误消息。什么似乎是问题?

1 个答案:

答案 0 :(得分:1)

试试这个对我有用

    $con = mysql_connect(<host>,<username>,<password>) or die(mysql_error());
    mysql_select_db(<database_name>,$con);
    $usernametable="test".$usname;
    $create = ("CREATE TABLE $usernametable(
    id SMALLINT NOT NULL PRIMARY KEY,
    test1 SMALLINT, test2 SMALLINT, test3 SMALLINT )");


    $createtable = mysql_query($create, $con) 
    or die ('Problem with query' . mysql_error());