php mysql 增删改查 封装_PHP简单封装,实现增删改查

以下是对PHP连接数据进行的简单封装代码,能够实现对数据库的增删改查

/**

* mysql connection configure

* 2016-04-10

* Ron

*/

class DBUtils{

private $ychat_host = "localhost";

private $ychat_username = "root";

private $ychat_password = "12345";

private $ychat_database = "talk";

private $conn;

/*

function __construct($dbhost,$dbusername,$password,$database){

$this->ychat_host=$dbhost;

$this->ychat_username=$dbusername;

$this->ychat_password=$password;

$this->ychat_database=$database;

$this->connect();

}*/

function __construct(){

$this->connect();

}

function connect(){

$this->conn = mysql_connect($this->ychat_host,$this->ychat_username,$this->ychat_password)

or die ("connection MySQL failed!");

mysql_select_db($this->ychat_database,$this->conn);

mysql_query("set names utf8");

}

function dbClose(){

mysql_close($this->conn);

}

//对mysql_query()、mysql_fetch_array()、mysql_num_rows()函数进行封装

function query($sql){

return mysql_query($sql);

}

function myArray($result){

return mysql_fetch_array($result);

}

function rows($result){

return mysql_num_rows($result);

}

//自定义查询数据方法

function select($table,$condition){

return $this->query("SELECT * FROM $table $condition");

}

function insert($table,$fields,$value){

$this->query("INSERT INTO $table $fields VALUES $value");

}

function update($table,$change,$condition){

$this->query("UPDATE $table SET $change $condition");

}

function delete($table,$condition){

$this->query("DELETE FROM $table $condition");

}

}

?>

用test.php来进行测试

header("Content-type:text/html;charset=utf-8");

// include('../controller/connect.php');

require("../controller/connect.php");

$db = new DBUtils();

/*

$delete = $db->delete("userinfo", "where id = 8");

echo "delete:".$delete;

$db->dbClose();*/

// $db->insert("userinfo", "(username,password)", "('Kim nana','123456')");

$select = $db->select("userinfo", "");

$row = $db->rows($select);

echo $row;

?>

idusernamesex

while ($array = $db->myArray($select)) {

echo "

";

echo "

".$array['id']."";

echo "

".$array['username']."";

echo "

".$array['sex']."";

echo "

";

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值