21xrx.com
2024-12-23 16:40:11 Monday
登录
文章检索 我的文章 写文章
PHP MySQL创建表
2021-07-24 10:07:17 深夜i     --     --
PHP MySQL创建表


数据库表有自己的独特名称,包括 列和行。


使用mysqli和pdo创建MySQL表

CREATE TABLE语句用于在MySQL中创建一个表。

我们将创建一个名为“myguests”的表格 五列:“id”,“firstname”,“lastname”,“email”和“reg_date”:

CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

上面的表格:

数据类型指定列可以保存的数据类型。 为了完整引用所有可用数据类型,转到我们的数据类型引用。

数据类型后,您可以为每个标识其他可选属性:

  • NOT NULL  - 每行必须包含该列的值,不允许空值
  • DEFAULT - 设置未通过其他值时添加的默认值
  • UNSIGNED - 用于数字类型,将存储的数据限制为正数和零
  • AUTO INCREMENT - MySQL每次添加新记录时都会自动增加字段的值
  • PRIMARY KEY - 用于唯一地标识表中的行。 具有主键设置的列通常是ID号,通常与auto_increment一起使用

每个表应具有主键列(在本例中:“ID”列)。 其值对于表中的每个记录必须是唯一的。

以下示例显示了如何在PHP中创建表:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
  echo "Table MyGuests created successfully";
} else {
  echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

 



<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if (mysqli_query($conn, $sql)) {
  echo "Table MyGuests created successfully";
} else {
  echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


 

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // sql to create table
  $sql = "CREATE TABLE MyGuests (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  firstname VARCHAR(30) NOT NULL,
  lastname VARCHAR(30) NOT NULL,
  email VARCHAR(50),
  reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  )";

  // use exec() because no results are returned
  $conn->exec($sql);
  echo "Table MyGuests created successfully";
} catch(PDOException $e) {
  echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>


 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章