如题
我现在想通过php代码来为数据库里添加一张表与删除一张表急求。。

解决方案 »

  1.   


    $sql = "DROP TABLE IF EXISTS `user`";//执行会删除 user 表$sql = "CREATE TABLE `user` (
      `id` mediumint(8) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8";//执行回添加user表可以装个工具,phpmyadmin就不错,
      

  2.   

    <?php
    // we connect to example.com and port 3307
    $link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($link);// we connect to localhost at port 3307
    $link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }else{
    // 建立表
    $query = "CREATE TABLE `TableName` (
      `id` int(11) NOT NULL auto_increment,
      `title` varchar(20) NOT NULL,
      `content` mediumtext NOT NULL,
      `keyw` varchar(20) NOT NULL,
      `author` varchar(20) NOT NULL,
      `lastdate` datetime NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=75";

    /*
    // 如果删除表,就用下面这个语句
    $query = "DROP TABLE IF EXISTS `TableName`";
    */
    $result = mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    }
    mysql_close($link);
    ?>
      

  3.   

    执行sql语句就行了,只不过sql语句是 create table 而己。