dougou7008 2017-05-19 13:18
浏览 103
已采纳

如果它不存在它如何创建一个表使用PHP的数据库?

I am tring to create a new database and new table if they dont exists in the sql. when i execute this code twice it works fine but when i execute it first time it does not create the table. the below is the code that i am using

$dbhost = '7.0.0.1';
$dbuser = 'root';
$dbpass = '123';
$dbname = 'promo_gmail';
$maxcharinaline = 1000;
$delimeter      = ',';
$usertb = "promo_userb";
$typetb = "promo_typeb";
// Now just insert the data one by one assuming its valid
{
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);
        if(!$conn)
        {
                die('Failed to connect to server: ' . mysql_error());
                exit;
        }
        $db_selected = mysql_select_db($dbname);
        if(!$db_selected)
        {
               $db_selected = "CREATE DATABASE $dbname";
        }
        mysql_query($db_selected,$conn);
        $usertb1 = mysql_query("select 1 from $usertb LIMIT 1");
        if( $usertb1 == FALSE )
        {
               $usertb1 = "CREATE TABLE IF NOT EXISTS $usertb ( uid INT(20) AUTO_INCREMENT UNIQUE KEY NOT NULL,name VARCHAR(40),email varchar(40) PRIMARY KEY NOT NULL)";
               mysql_query($usertb1,$conn);

        }
        $typetb1 = mysql_query("select 1 from $typetb LIMIT 1");
        if( $typetb1 == FALSE )
        {
               $typetb1 = "CREATE TABLE IF NOT EXISTS $typetb ( uid INT(20) , type ENUM('WEB','APP','AT') NOT NULL ,unsubscribe TINYINT(1) NOT NULL,bounce TINYINT(1) NOT NULL,complaint TINYINT(1) NOT NULL, FOREIGN KEY (uid) REFERENCES promo_user (uid) , PRIMARY KEY (uid,type))";
               mysql_query($typetb1,$conn);
        }

please Help.

  • 写回答

1条回答 默认 最新

  • dtkyayvldeaqhl7151 2017-05-19 13:36
    关注

    When you run this the first time, you are checking if the db exists and if not exists you are creating one. But I think you need to do a

    $db_selected = mysql_select_db($dbname);
    

    after creating the database.

    So you snippet will change to

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(!$conn)
    {
            die('Failed to connect to server: ' . mysql_error());
            exit;
    }
    $db_selected = mysql_select_db($dbname);
    if(!$db_selected)
    {
           $db_selected = "CREATE DATABASE $dbname";
    }
    //Create the new database          
    mysql_query($db_selected,$conn);
    //Select the new database
    $db_selected = mysql_select_db($dbname);
    //Go ahead with your table queries
    $usertb1 = mysql_query("select 1 from $usertb LIMIT 1");
    if( $usertb1 == FALSE )
    {
           $usertb1 = "CREATE TABLE IF NOT EXISTS $usertb ( uid INT(20) AUTO_INCREMENT UNIQUE KEY NOT NULL,name VARCHAR(40),email varchar(40) PRIMARY KEY NOT NULL)";
           mysql_query($usertb1,$conn);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系