PHP-操作Mysql

连接mysql

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 15:36
 */

    // 连接mysql,@可以让错误不显示出来
    $conn = @mysqli_connect('127.0.0.1','root','root') or die('服务器连接失败!错误信息:'.mysqli_error());

?>

 可以直接定义Mysql连接相关信息的常量来进行连接,这样更安全

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 15:36
 */

    // 设置常量,这样可以更安全
    define('DB_HOST', '127.0.0.1');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', 'root');

    $conn = @mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die('服务器连接失败!错误信息:'.mysqli_error());

?>

 连接mysql数据库并对表中的数据进行查询

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 15:36
 */

	// 设置常量,这样可以更安全
    define('DB_HOST', '127.0.0.1');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', 'root');
    define('DB_NAME', 'test'); 

    $conn = @mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die('服务器连接失败!错误信息:'.mysqli_error());

    @mysqli_select_db($conn, DB_NAME) or die('数据库连接失败!错误信息:'.mysqli_error());

    mysqli_query($conn, 'SET NAMES UTF8') or die('编码设置失败'); // 设置编码,解决中文乱码
    
    $query = "SELECT * FROM admin";

    $result = @mysqli_query($conn, $query) or die('表中数据查询失败');
    print_r(mysqli_fetch_array($result, MYSQLI_ASSOC));  // 按照字段名作为关联数组
    // MYSQLI_NUM 以数字作为关联数组
    // 返回的结果为资源句柄

    mysqli_free_result($result);  // 释放结果内存
    mysqli_close($conn)  // 关闭$conn的连接

?>

 运行结果

增删改查

包含config.php

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 16:17
 */

    define('DB_HOST','127.0.0.1');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', 'root');
    define('DB_NAME', 'test');

    $conn = @mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die ('服务器连接失败,'.mysqli_error());

    @mysqli_select_db($conn, DB_NAME) or die('数据库连接失败,'.mysqli_error());

?>

 新增数据

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 16:18
 */

    include 'config.php';

    $query = "INSERT INTO admin (username,password) VALUES ('phpuser', 'phppwd')";

    @mysqli_query($conn, $query) or die('增加数据错误,'.mysqli_error());

    mysqli_close($conn);

?>

 运行结果

删除数据

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 16:27
 */

    include 'config.php';

    $query = 'DELETE FROM admin WHERE id=9';

    @mysqli_query($conn, $query) or die('删除数据失败,'.mysqli_error());

    mysqli_close($conn);

?>

 运行结果

修改数据

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 16:21
 */

    include 'config.php';

    $query = "UPDATE admin SET password='testpwd' WHERE id=2";

    @mysqli_query($conn, $query) or die('添加数据失败');
    mysqli_close($conn)

?>

 运行结果

查询数据

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 16:30
 */

    include 'config.php';
    $query = 'SELECT * FROM admin';
    $result = @mysqli_query($conn, $query) or die('数据查询失败,'.mysqli_error());

    while ($row = mysqli_fetch_array($result)){
       echo 'id:'.$row['id']."-----".'username:'.$row['username']."-----".'password:'.$row['password'];
       echo "\n";
   }

    mysqli_close($conn);

?>

 运行结果

Mysql操作的其它的一些方法

<?php
/**
 * Author: MuT6 Sch01aR
 * Date: 2018/3/16
 * Time: 21:31
 */

    include 'config.php';

    $query = 'SELECT * FROM admin';

    $result = @mysqli_query($conn, $query) or die('表中数据查询失败');
    print_r(mysqli_fetch_array($result, MYSQLI_NUM));

    print_r(mysqli_fetch_row($result));  // 从结果集中取得一行作为数字数组
    print_r(mysqli_fetch_assoc($result));  // 从结果集中取得一行作为关联数组
    // mysqli_fetch_array()集成了他们两的功能

    echo mysqli_num_rows($result);  //获取表中有多少条数据
    echo "\n";
    echo mysqli_num_fields($result);  // 打印表中的字段数
    echo "\n";
    echo mysqli_get_server_info($conn);  //获取mysql服务器信息
    echo "\n";
    echo mysqli_get_host_info($conn);  //获取mysql主机信息
    echo "\n";
    echo mysqli_get_proto_info($conn);  //获取mysql协议信息
    echo "\n";
    echo mysqli_get_client_info($conn);  //获取连接mysql的客户端版信息

    mysqli_close($conn);

?>

 运行结果

 

posted @ 2018-03-16 21:40  Sch01aR#  阅读(180)  评论(0编辑  收藏  举报