zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password);
    
    // 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    echo "连接成功";
    ?>

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    
    try {
        $conn = new PDO("mysql:host=$servername;", $username, $password);
        echo "连接成功";
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    ?>

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password);
    // 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    // 创建数据库
    $sql = "CREATE DATABASE myDB";
    if ($conn->query($sql) === TRUE) {
        echo "数据库创建成功";
    } else {
        echo "Error creating database: " . $conn->error;
    }
    
    $conn->close();
    ?>

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);
    // 检测连接
    if (!$conn) {
        die("连接失败: " . mysqli_connect_error());
    }
     
    // 创建数据库
    $sql = "CREATE DATABASE myDB";
    if (mysqli_query($conn, $sql)) {
        echo "数据库创建成功";
    } else {
        echo "Error creating database: " . mysqli_error($conn);
    }
     
    mysqli_close($conn);
    ?>
    try { 
        $conn = new PDO("mysql:host=$servername", $username, $password); 
    
        // 设置 PDO 错误模式为异常 
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        $sql = "CREATE DATABASE myDBPDO"; 
    
        // 使用 exec() ,因为没有结果返回 
        $conn->exec($sql); 
    
        echo "数据库创建成功<br>"; 
    } 
    catch(PDOException $e) 
    { 
        echo $sql . "<br>" . $e->getMessage(); 
    } 
    
    $conn = null; 
    ?>
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $dbname = "myDB";
    
    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
    
    // 使用 sql 创建数据表
    $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
    )";
    
    if ($conn->query($sql) === TRUE) {
        echo "Table MyGuests created successfully";
    } else {
        echo "创建数据表错误: " . $conn->error;
    }
    
    $conn->close();
    ?>

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // 检测连接
    if (!$conn) {
        die("连接失败: " . mysqli_connect_error());
    }
     
    // 使用 sql 创建数据表
    $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
    )";
     
    if (mysqli_query($conn, $sql)) {
        echo "数据表 MyGuests 创建成功";
    } else {
        echo "创建数据表错误: " . 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);
        // 设置 PDO 错误模式,用于抛出异常
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     
        // 使用 sql 创建数据表
        $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
        )";
     
        // 使用 exec() ,没有结果返回 
        $conn->exec($sql);
        echo "数据表 MyGuests 创建成功";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }
     
    $conn = null;
    ?>
  • 相关阅读:
    Jmeter正则表达式提取器(转载)
    Jmeter(三十九)获取响应结果中参数出现的次数(转载)
    JMeter3.0(三十八)图形化HTML报告中文乱码问题处理(转载)
    JMeter学习(三十七)Jmeter录制手机app脚本(转载)
    JMeter学习(三十六)发送HTTPS请求(转载)
    JMeter学习(三十四)使用jmeter来发送json/gzip格式数据(转载)
    用BlazeMeter录制JMeter(三十五)测试脚本(转载)
    JMeter学习(三十三)测试报告优化(转载)
    Jmeter学习(三十二)调试工具Debug Sampler(转载)
    ‘cmake' 不是内部或外部命令 也不是可运行的程序 或批处理文
  • 原文地址:https://www.cnblogs.com/tszr/p/10955464.html
Copyright © 2011-2022 走看看