<?php
    $db_host   = 'localhost:3306';
    $db_user   = 'root';
    $db_passwd = '';
    $db_name   = 'test';


    $conn = mysql_connect($db_host,$db_user,$db_passwd);
    mysql_set_charset('utf8');//设置数据字体编码
    if(!$conn){
        die('Could not connect:'.mysql_error());
    }
    mysql_select_db($db_name);//选择连接的数据表

    //查询数据库函数
    function select($table,$where='',$order='',$limits='',$field=''){
        $field = ($field==null)?'*':$field;
        $where = ($where==null)?'':' WHERE '.$where;
        $order = ($order==null)?'':' ORDER BY '.$order;
        $limits = ($limits==null)?'':' LIMIT '.$limits;
        $sele = 'SELECT'.$field.' FROM '.$table.$where.$order.$limits;
        return mysql_query($sele);
        mysql_close($conn); //关闭数据库链接

    }

    //插入数据库函数,第二个参数必须为数组 
    function insert($table,$columns){
        foreach ($columns as $key => $values) {
            $addkey .= '`'.$key.'`,';
            if(is_numeric($values)){ //判断是不是为数字
                $addvalues .= $values.',';
            }else{
                $addvalues .= '\''.mysql_real_escape_string($values).'\','; //转义sql语句字符
            }
        }
            $addkey = rtrim($addkey,','); //删除字符串末尾的指定字符
            $addvalues = rtrim($addvalues,',');
            $inse = 'INSERT INTO '.$table.'('.$addkey.') VALUES ('.$addvalues.');';
            return mysql_query($inse);
            mysql_close($conn); //关闭数据库链接
    }

    //更新数据库函数,第二个参数必须为数组
    function update($table,$data,$where){
        $where = ($where==null)?'':' WHERE '.$where;
        foreach ($data as $k => $v) {
            if(is_numeric($v)){
                $edit_data .= '`'.$k.'`'.'='.$v.',';
            }else{
                $edit_data .= '`'.$k.'`'.'='."'".mysql_real_escape_string($v)."',";
            }
        }  
         $edit_data = rtrim($edit_data,',');
         $upda = 'UPDATE '.$table.' SET '.$edit_data.$where;
    var_dump($upda);
         return mysql_query($upda);
        mysql_close($conn); //关闭数据库链接
    }

    //删除数据库数据函数
   function delete($table,$where){
        $where = ($where==null)?'':' WHERE '.$where;
        $dele = 'DELETE FROM '.$table.$where;
        return mysql_query($dele);
        mysql_close($conn); //关闭数据库链接
    }
      

mysql增删查改的更多相关文章

  1. php mysql增删查改

    php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root');  //连接数据库代码 mysql_query("set na ...

  2. mysql 增删查改

    非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...

  3. node.js+mysql增删查改

    数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...

  4. 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)

    1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...

  5. 后端Spring Boot+前端Android交互+MySQL增删查改

    2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...

  6. MySQL 增删查改 必知必会

    MySQL 数据库中的基础操作 3.表的修改 对表的表名.字段.字段类型.字段长度.约束等进行修改. 3.1 表的名称修改 -- 语法: ALTER TABLE 库名.表名 RENAME TO 新表名 ...

  7. python操作mysql增删查改

    # coding=utf-8 ''' python操作mysql,需安装MySQLdb驱动 安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-py ...

  8. 2016/3/13 MySQL 增删查改 CRUD 用代码实现

    用代码实现数据库数据输入 T-SQL语句 查询语句分几块 ①创建表 create table Car (Code varchar(50) primary key, #primary key 主键 定义 ...

  9. MySQL增删查改语句(入门)

    目录 create alter: insert delete update select 数据库定义语句: create:创建数据库及表对象 drop:删除数据库及表对象 alter:修改数据库及表对 ...

  10. mysql增删查改练习

    建表 班级表 create table class( cid int auto_increment unique, caption varchar(32) not null default '' )c ...

随机推荐

  1. InvalidOperationException: Cannot create a DbSet for 'IdentityUserClaim<string>' because this type is not included in the model for the context.

    An unhandled exception occurred while processing the request. InvalidOperationException: Cannot crea ...

  2. PostAsync与GetAsync

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...

  3. Codeforces Round #585 (Div. 2) CF1215A~C

    CF1215A. Yellow Cards简单的模拟,给定了黄票张数,判断最少和最多有多少人被罚下场. #include <bits/stdc++.h> using namespace s ...

  4. js原型链理解(2)--原型链继承

    1.原型链继承 2.constructor stealing(构造借用) 3.组合继承 js中的原型链继承,运用的js原型链中的__proto__. function Super(){ this.se ...

  5. 第一部分 JavaScript语言核心(四)

    第九章 类和模块 P235 组合VS子类:组合优于继承 P236 类的层次结构和抽象类 第十章 正则达式的模式匹配 定义 JS中正则表达式用RegExp对象表示.直接量定义为包含在一堆斜杠(/)之间的 ...

  6. npm - 换淘宝源

    npm - 换淘宝源Node 的模块管理器 npm 会一起安装好.由于 Node 的官方模块仓库网速太慢,模块仓库需要切换到阿里的源. $ npm config set registry https: ...

  7. redis(四)----发布订阅

    发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合.pub /sub不仅仅解决发布者和订阅者直接代码级别耦合,也解决两者在物理部署上的耦合.废话不多说,直接 ...

  8. POJ 2362:Square 觉得这才算深度搜索

    Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21821   Accepted: 7624 Descripti ...

  9. 基于百度语音识别API的Python语音识别小程序

    一.功能概述 实现语音为文字,可以扩展到多种场景进行工作,这里只实现其基本的语言接收及转换功能. 在语言录入时,根据语言内容的多少与停顿时间,自动截取音频进行转换. 工作示例: 二.软件环境 操作系统 ...

  10. HTTP协议(四):首部

    前言 作者说:上一节中介绍了HTTP报文中的状态码,这一节同样是对报文的补充,介绍的是HTTP首部字段.不过,你如果是第一次见到这个东西,肯定会特别疑惑,什么是HTTP首部? <图解HTTP&g ...