博客列表 >0321作业

0321作业

Z-X-P的博客
Z-X-P的博客原创
2019年03月31日 22:55:12562浏览

<?php

//pdo数据库操作作业

//增删改查

//1连接数据库

function con(){

//地址 使用默认端口3306

$dsn = 'mysql:host=127.0.0.1;dbname=user;charset=utf8;port=3306';

//数据库账号

$dbname = 'root';

//数据库密码

$dbpw = 'root';

//try处理链接错误,并抛出一个PDO异常对象

try{

$p = new PDO($dsn,$dbname,$dbpw);

}catch(PDOException $e){

print_r($e->getMessage());

exit();

}

return $p;

}


//查询方法

function select($table,$field='*',$where='',$order='',$limit=''){

//连接数据库

$c = con();

//查询语句 SELECT * FROM `user1`  where `id` order by id limit 1

$sql = "SELECT ";

if(!empty($field))

{

$sql .= $field;

}


$sql .= " FROM ";

$sql .= $table;


if(!empty($where))

{

$sql .= " WHERE ";

$sql .= $where;

}


if(!empty($order))

{

$sql .= " ORDER BY ";

$sql .= $order;

}


if(!empty($limit))

{

$sql .= " LIMIT ";

$sql .= $limit;

}


$a = $c->prepare($sql);

if($a->execute()){

$a->setFetchMode(PDO::FETCH_ASSOC);

$ret = $a->fetchALL();

return $ret;

}else{

return false;

}


}

// $s = select('user1','`id`,`username`,`py`','`id` > 2','id','2,1');


//插入

function insert($table='',$field='',$value=''){

//连接数据库

$c = con();


//插入语句

//INSERT INTO table_name (field1,field2) value(field1,field2)

$sql = "INSERT INTO ";


if(!empty($table))

{

$sql .= $table;

}


if(!empty($field))

{

$sql .= "(";

$sql .= $field;

$sql .= ")";

}


if(!empty($value))

{

$sql .= " VALUE (";

$sql .= $value;

$sql .= ")";

}


$a = $c->prepare($sql);

if($a->execute()){

$a->setFetchMode(PDO::FETCH_ASSOC);

$ret = $a->fetchALL();

return $ret;

}else{

return false;

}

}

// $s = insert('`user1`','`username`,`py`,`password`,`email`',"'啊啊啊','aaa','asd123','12321a@qq.com'");


//修改

function update($table='',$field='',$value='',$where=''){

$c = con();

//修改mysql语句

//UPDATE TABLE_NAME SET field=new - value1 whwere

$sql = "UPDATE ";


if(!empty($table))

{

$sql .= $table;

}


if(!empty($field))

{

$sql .= " SET ";

$sql .= $field;

}


if(!empty($value))

{

$sql .= ' = '.$value;

}


if(!empty($where))

{

$sql .= " WHERE ";

$sql .= $where;

}



$a = $c->prepare($sql);

if($a->execute()){

$a->setFetchMode(PDO::FETCH_ASSOC);

$ret = $a->fetchALL();

return $ret;

}else{

return false;

}



}

// $u = update('user1','username',"'受不了啊'",'id=3');


function delete($table='',$where=''){

$c = con();

//DELETE FROM TABLE_NAME[WHERE ]

$sql = 'DELETE FROM ';

if(!empty($table))

{

$sql .= $table;

}


if(!empty($where))

{

$sql .=  ' WHERE '.$where;

}else{

exit();

}

echo $sql;


$a = $c->prepare($sql);

if($a->execute()){

$a->setFetchMode(PDO::FETCH_ASSOC);

$ret = $a->fetchALL();

return $ret;

}else{

return false;

}

}

// $d = delete('user1','id=3');

?>

mysql数据


DROP TABLE IF EXISTS `user1`;

CREATE TABLE `user1` (

  `id` smallint(6) NOT NULL AUTO_INCREMENT COMMENT 'id',

  `username` varchar(255) DEFAULT NULL COMMENT '用户名',

  `password` varchar(255) DEFAULT NULL COMMENT '密码',

  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',

  `py` varchar(50) DEFAULT NULL,

  `age` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;


-- ----------------------------

-- Records of user1

-- ----------------------------

INSERT INTO `user1` VALUES ('1', '王大锤', '吐血', 'tuxue@asd.com', 'wangdachui', '40');

INSERT INTO `user1` VALUES ('2', '保加利亚妖王', '321212', 'yaowang@123.com', 'yaowang', '50');

INSERT INTO `user1` VALUES ('4', '保加利亚', '32112', '123yaowang@123.com', 'baojialiya', '30');

INSERT INTO `user1` VALUES ('6', '法克二', 'fake', 'fake@163.com', 'fakjer', '20');

INSERT INTO `user1` VALUES ('7', '玩玩啊', 'asds123', 'asdsweq@qq.com', 'wanwana', '55');

INSERT INTO `user1` VALUES ('8', '啊啊啊', 'asd123', '12321a@qq.com', 'aaa', '44');



声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议