• php7 数据库操作的 方法


    连接数据库的方法PHP7.0以上的:

    方法一:

    <?php
    /* Connect to a MySQL server 连接数据库服务器 */
    $link = mysqli_connect(
    'localhost', /* The host to connect to 连接MySQL地址 */
    'root', /* The user to connect as 连接MySQL用户名 */
    'root', /* The password to use 连接MySQL密码 */
    'lucax_database'); /* The default database to query 连接数据库名称*/

    if (!$link) {
    printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
    exit;
    }else
    echo '数据库连接上了!';

    /* Close the connection 关闭连接*/
    mysqli_close($link);
    ?>

    https://www.cnblogs.com/jianqingwang/p/5890111.html

    方法二:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "root";

    $dbname = "myDB";

    // 创建连接

    Tip: 如果你使用其他端口(默认为3306),为数据库参数添加空字符串,如: new mysqli("localhost", "username", "password", "", port)
    $conn = new mysqli($servername, $username, $password,$dbname);

    // 检测连接
    if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
    }
    echo "连接成功";

    $conn->close();
    ?>

    http://www.runoob.com/php/php-mysql-connect.html

    创建数据库:

    方法一:

    $sql = "CREATE DATABASE myDB3";
    if (mysqli_query($conn, $sql)) {
    echo "数据库创建成功";
    } else {
    echo "Error creating database: " . mysqli_error($conn);
    }

    方法二:

    // 检测连接
    if ($conn->connect_error) {
      die("连接失败: " . $conn->connect_error);
    }
    echo "连接成功";

    $sql = "CREATE DATABASE myDB2";
    if ($conn->query($sql) === TRUE) {
      echo "数据库创建成功";
    } else {
      echo "Error creating database: " . $conn->error;
    }

    http://www.runoob.com/php/php-mysql-create.html

    创建表的方法:

    方法一:

    // 使用 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);
    }

    方法二:

    // 使用 sql 创建数据表
    $sql = "CREATE TABLE MyGuests2 (
    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;
    }

    http://www.runoob.com/php/php-mysql-create-table.html

    插入数据的方法:

    方法一:

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";

    if (mysqli_query($conn, $sql)) {
    echo "新记录插入成功";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    方法二:

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";

    if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    http://www.runoob.com/php/php-mysql-insert.html

    插入多条数据的方法:

    方法一:

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Mary', 'Moe', 'mary@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Julie', 'Dooley', 'julie@example.com')";

    if (mysqli_multi_query($conn, $sql)) {
    echo "新记录插入成功";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    方法二:

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('22', 'Doe', 'john@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('22', 'Moe', 'mary@example.com');";
    $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('22', 'Dooley', 'julie@example.com')";

    if ($conn->multi_query($sql) === TRUE) {
    echo "新记录插入成功";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    查询数据库的方法:

    方法一:

    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
    } else {
    echo "0 结果";
    }

    方法二:

    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
    } else {
    echo "0 结果";
    }

    http://www.runoob.com/php/php-mysql-select.html

    增、删、改、查 都是使用 mysqli_query() 函数

    增:http://www.runoob.com/php/php-mysql-insert.html

    删:http://www.runoob.com/php/php-mysql-delete.html

    改: http://www.runoob.com/php/php-mysql-update.html

    查: http://www.runoob.com/php/php-mysql-select.html

  • 相关阅读:
    Tomcat域名绑定
    Windows下搭建PHP开发环境
    创业项目该如何选择技术?
    linux mount 挂接新硬盘
    Linux 查看系统硬件信息
    this super的用法
    构造方法
    多态
    抽象类和接口
    继承
  • 原文地址:https://www.cnblogs.com/kaibindirver/p/10418620.html
一二三 - 开发者的网上家园