【问题标题】:How to create multiple tables in same database using php? [closed]如何使用php在同一个数据库中创建多个表? [关闭]
【发布时间】:2015-08-04 18:01:32
【问题描述】:

我正在尝试在名为“new”的同一个数据库中创建多个表,我想知道我们可以在查询代码中使用 php 来提供多个表名吗? 在此先感谢...

<?php
$a=0;
$con=new mysqli("localhost","root","","new") or die("Can not connect to database");
$sql=("create table table.'echo $a++;' ( num int, name varchar(50))");
$con->query($sql);
$con->close();
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

【问题讨论】:

  • 你的问题是什么?
  • 一件事的回声会给你的一天带来阻碍。您也没有使用$a 循环任何内容。 - $a++ 到多少次和多少次迭代?
  • php 不是递归可嵌入/可执行的...所以,不,您不能使用此代码。你不能编写自己的语法并期望它“正常工作”
  • 可以让它进入一个while循环,比如while($a

标签: php html mysql mysqli


【解决方案1】:

试试这个:

<?php
$a=0;
$con=new mysqli("localhost","root","","gcm") or die("Can not connect to database");
$a=0;
while($a<3){
$sql=("create table table". $a ." ( num int, name varchar(50))");
    $a++;
$con->query($sql);
}

//$con->query($sql);
$con->close();
?>

无论如何,您必须执行多个创建表 SQL 语句,这些语句我放在一个 while 循环中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多