dongqin6926 2014-12-15 20:34
浏览 31
已采纳

如何使用php创建mysql表

i need to make application installation and need to make this tables in installation like wordpress. i have not error in php but this codes doesnt make any tables...

$conn = new mysqli('localhost', 'root', '', 'billboard');
if (mysqli_connect_errno()) {
    exit('Connection lost');
};
for($i=1;$i<=21;$i++){
    $table= "city".$i ;
    echo ($table . "<br>");
    $sql='CREATE TABLE $table
      ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),
        test0 VARCHAR(30),
        test1 VARCHAR(30),
        test2 VARCHAR(30))';
    if ($conn->query($sql) === TRUE) {
        echo 'tables created';
    }
    else {
        echo 'Cant create tables';
    };
};
  • 写回答

1条回答 默认 最新

  • doudilin1225 2014-12-15 20:40
    关注

    PHP evaluates variables in strings that are "wrapped with double quotes", but does not evaluate variables when strings are 'wrapped in single quotes'. In other words, single-quoted strings are literal.

    In this case, you're attempting to create a table called $table 20 times. Instead, you can concatenate the variable into the loop like so:

    $sql= 'CREATE TABLE ' . $table . '
        ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),
        //etc...
    

    OR

    $sql= "CREATE TABLE $table 
        ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),
        //etc...
    

    I find it's more convenient to use double quotes for SQL queries anyway, since you will often need to put SQL values into single quotes (e.g. WHERE ID = '5').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!