Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中为MySQL服务器(MySQLi)创建表_Php_Mysql_Mysqli - Fatal编程技术网

在PHP中为MySQL服务器(MySQLi)创建表

在PHP中为MySQL服务器(MySQLi)创建表,php,mysql,mysqli,Php,Mysql,Mysqli,连接: <?php $db_name = "xxx"; $mysql_username = "xxx"; $mysql_password = "xxx"; $server_name = "xxx"; // Create connection $conn = new mysqli("xxx","xxx","xxx","xxx"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect

连接:

<?php 
$db_name = "xxx";
$mysql_username = "xxx";
$mysql_password = "xxx";
$server_name = "xxx";
// Create connection
$conn = new mysqli("xxx","xxx","xxx","xxx");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

完整代码(这是注册代码)[编辑](我越来越近了吗?)


我使用这段代码创建了一个表,表名为发布的用户名。这有什么问题,因为它不起作用

$MyServer = ($_POST["username"]);
$sql = "CREATE TABLE '$MyServer' (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(30) NOT NULL
    )";
$result = mysqli_query($con,$sql);
mysqli_查询(连接对象,查询)


您可以按上述方式执行查询。

这只是我个人的喜好

$myServer = mysqli_real_escape_string($con, $_POST["username"]);
$sql = "CREATE TABLE '$MyServer' (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(30) NOT NULL
    )";
$result = mysqli_query($conn, $sql);
PDO也是一种选择

$host = 'localhost';
$db   = '<INSERT DATABASE NAME HERE>';
$user = '<INSERT USERNAME HERE>';
$pass = '<INSERT PASSWORD HERE>';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";

$pdo = new PDO($dsn, $user, $pass, $opt);

$stmt = $pdo->prepare('CREATE TABLE ? (
        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
        username VARCHAR(30) NOT NULL
        )');
$stmt->execute([$myServer]);
$success = $stmt->fetch();
$host='localhost';
$db='';
$user='';
$pass='';
$charset='utf8';
$dsn=“mysql:host=$host;dbname=$db;charset=$charset”;
$pdo=新pdo($dsn,$user,$pass,$opt);
$stmt=$pdo->prepare('创建表(
id INT(6)无符号自动递增主键,
用户名VARCHAR(30)不为空
)');
$stmt->execute([$myServer]);
$success=$stmt->fetch();

使用以下行作为您的sql值

$sql = " create table if not exists  {$MyServer} (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(30) NOT NULL
    )";

在mysql中,表名中没有单引号('),而是`

到目前为止,您已经得到了一个包含SQL语句的字符串。你拿它干什么?我们需要您运行查询时使用的代码以及出现的错误。请输入您用于执行您提到的查询的代码。如何在MySQLi中执行查询?(我是初学者,请具体说明)
$sql = " create table if not exists  {$MyServer} (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    username VARCHAR(30) NOT NULL
    )";