zoukankan      html  css  js  c++  java
  • mysql与PHP建立连接实现增删查改

    mysql与PHP连接的查询写法:

    <?php     
    //1.建立与数据库的连接
    //类似于宽字符集问题,mysqli是额外的扩展
    //需要找到配置文件去开启扩展
    
    //如果需要在调用函数 之前忽略错误或者警告可以在函数名之前加上@ 
    
    $connection=mysqli_connect('127.0.0.1','root','root','demo');
    if (!$connection) {
        //连接数据库失败
        exit('<h1>连接数据库失败</h1>');
    }
    //基于刚刚创建的连接对象执行一次查询操作
    //得到的是一个查询对象,这个查询对象可以用来再到数据一行一行拿数据
    $query=mysqli_query($connection,'select * from user');
    if (!$query) {
        exit('<h1>查询失败</h1>');
    }
    
    //遍历结果集
    while ($row=mysqli_fetch_assoc($query)) {
        var_dump($row);
    }
    //释放结果集
    mysqli_free_result($query);
    //炸桥    关闭连接
    mysqli_close($connection);

    mysql与PHP连接的增删改数据的查询写法:

    <?php     
    //1.建立与数据库的连接
    //如果需要在调用函数 之前忽略错误或者警告可以在函数名之前加上@ 
    
    $connection=mysqli_connect('127.0.0.1','root','root','demo');
    if (!$connection) {
        //连接数据库失败
        exit('<h1>连接数据库失败</h1>');
    }
    //基于刚刚创建的连接对象执行一次查询操作
    //得到的是一个查询对象,这个查询对象可以用来再到数据一行一行拿数据
    $query=mysqli_query($connection,'delete from user where userId = 1');
    if (!$query) {
        exit('<h1>查询失败</h1>');
    }
    
    //如何拿到受影响行
    //传入的一定是受影响行
    $rows=mysqli_affected_rows($connection);
    var_dump($rows);
    //炸桥    关闭连接
    mysqli_close($connection);

     为了避免PHP中查询中文编码的问题需要在查询数据之前传入连接对象和编码,如下

    mysqli_set_charset($connection,'utf8');
  • 相关阅读:
    android基本控件学习-----Date&Time
    android基本控件学习-----ScrollView
    android基本控件学习-----SeekBar&RatingBar
    android基本控件学习-----ProgressBar
    android基本控件学习-----ToggleButton&Switch
    android基本控件学习-----RadioButton&CheckBox
    android基本控件学习-----ImageView
    android基本控件学习-----Button
    android基本控件学习-----EditText
    android基本控件学习-----TextView
  • 原文地址:https://www.cnblogs.com/Yaucheun/p/10440308.html
Copyright © 2011-2022 走看看