ikkhksvu 发表于 2024-7-10 16:29:46

php mysql数据库以及数据表的创建


    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">&lt;</span><span style="color: black;">meta</span> <span style="color: black;">charset</span><span style="color: black;">=</span><span style="color: black;">"UTF-8"</span><span style="color: black;">&gt;</span>
      <span style="color: black;">&lt;!--</span> <span style="color: black;">1</span> <span style="color: black;">数据库的创建</span><span style="color: black;">--&gt;</span>
      <span style="color: black;">&lt;?</span><span style="color: black;">php</span>
      <span style="color: black;">/*
      </span><span style="color: black;"> * Created by PhpStorm.
      </span><span style="color: black;"> * User: admirtor
      </span><span style="color: black;"> * Date: 2018/2/12
      </span><span style="color: black;"> * Time: 13:19
      </span><span style="color: black;"> */</span>
      <span style="color: black;">$servername</span> <span style="color: black;">=</span> <span style="color: black;">"localhost"</span><span style="color: black;">;</span>
      <span style="color: black;">$username</span> <span style="color: black;">=</span> <span style="color: black;">"root"</span><span style="color: black;">;</span>
      <span style="color: black;">$password</span> <span style="color: black;">=</span> <span style="color: black;">"root"</span><span style="color: black;">;</span>

      <span style="color: black;">// 创建连接
      </span><span style="color: black;">$conn</span> <span style="color: black;">=</span> <span style="color: black;">new</span> <span style="color: black;">mysqli</span><span style="color: black;">(</span><span style="color: black;">$servername</span><span style="color: black;">,</span> <span style="color: black;">$username</span><span style="color: black;">,</span> <span style="color: black;">$password</span><span style="color: black;">);</span>
      <span style="color: black;">// 检测连接
      </span><span style="color: black;">if</span> <span style="color: black;">(</span><span style="color: black;">$conn</span><span style="color: black;">-&gt;</span><span style="color: black;">connect_error</span><span style="color: black;">)</span> <span style="color: black;">{</span>
      <span style="color: black;">die</span><span style="color: black;">(</span><span style="color: black;">"连接失败: "</span> <span style="color: black;">.</span> <span style="color: black;">$conn</span><span style="color: black;">-&gt;</span><span style="color: black;">connect_error</span><span style="color: black;">);</span>
      <span style="color: black;">}</span>

      <span style="color: black;">// 创建数据库</span><span style="color: black;">$sql</span> <span style="color: black;">=</span> <span style="color: black;">"CREATE DATABASE myDB"</span><span style="color: black;">;</span>
      <span style="color: black;">if</span> <span style="color: black;">(</span><span style="color: black;">$conn</span><span style="color: black;">-&gt;</span><span style="color: black;">query</span><span style="color: black;">(</span><span style="color: black;">$sql</span><span style="color: black;">)</span> <span style="color: black;">===</span> <span style="color: black;">TRUE</span><span style="color: black;">)</span> <span style="color: black;">{</span>
      <span style="color: black;">echo</span> <span style="color: black;">"数据库创建成功"</span><span style="color: black;">;</span>
      <span style="color: black;">}</span> <span style="color: black;">else</span> <span style="color: black;">{</span>
      <span style="color: black;">echo</span> <span style="color: black;">"Error creating database: "</span> <span style="color: black;">.</span> <span style="color: black;">$conn</span><span style="color: black;">-&gt;</span><span style="color: black;">error</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>

      <span style="color: black;">$conn</span><span style="color: black;">-&gt;</span><span style="color: black;">close</span><span style="color: black;">();</span>
      <span style="color: black;">?&gt;</span><span style="color: black;">&lt;!--2 创建数据表--&gt;</span><span style="color: black;">&lt;?php
      </span><span style="color: black;">$servername = "localhost";
      </span><span style="color: black;">$username = "root";
      </span><span style="color: black;">$password = "root";
      </span><span style="color: black;">$dbname = "myDB";
      </span><span style="color: black;">// 创建连接
      </span><span style="color: black;">$conn = new mysqli($servername, $username, $password, $dbname);</span><span style="color: black;">// 检测连接
      </span><span style="color: black;">if ($conn-&gt;connect_error) {
      </span><span style="color: black;"> die("连接失败: " . $conn-&gt;connect_error);
      </span><span style="color: black;">}
      </span><span style="color: black;">// <span style="color: black;">运用</span> sql 创建数据表
      </span><span style="color: black;">$sql = "CREATE TABLE MyGuests (
      </span><span style="color: black;">id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,</span><span style="color: black;">firstname VARCHAR(30) NOT NULL,
      </span><span style="color: black;">lastname VARCHAR(30) NOT NULL,
      </span><span style="color: black;">email VARCHAR(50),
      </span><span style="color: black;">reg_date TIMESTAMP
      </span><span style="color: black;">)";
      </span><span style="color: black;">if ($conn-&gt;query($sql) === TRUE) {</span><span style="color: black;"> echo "Table MyGuests created successfully";
      </span><span style="color: black;">} else {
      </span><span style="color: black;"> echo "创建数据表错误: " . $conn-&gt;error;
      </span><span style="color: black;">}
      </span><span style="color: black;">$conn-&gt;close();
      </span><span style="color: black;">?&gt;
      </span>
    </div>




b1gc8v 发表于 2024-10-5 16:06:06

我完全同意你的观点,说得太对了。

wrjc1hod 发表于 2024-10-24 03:55:49

i免费外链发布平台 http://www.fok120.com/

nqkk58 发表于 3 天前

你的见解独到,让我受益匪浅,非常感谢。
页: [1]
查看完整版本: php mysql数据库以及数据表的创建