数据库操纵基本流程为:
1、连接数据库服务器
2、选择数据库
3、执行SQL语句
4、处理结果集
5、打印操作信息
其中用到的相关函数有
"codetitle">复制代码 代码如下:
class mysql {
private $db_host; //数据库主机
private $db_user; //数据库登陆名
private $db_pwd; //数据库登陆密码
private $db_name; //数据库名
private $db_charset; //数据库字符编码
private $db_pconn; //长连接标识位
private $debug; //调试开启
private $conn; //数据库连接标识
private $msg = ""; //数据库操纵信息
// private $sql = ""; //待执行的SQL语句
public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_name = $db_name;
$this->db_charset = $db_chaeset;
$this->db_pconn = $db_pconn;
$this->result = '';
$this->debug = $debug;
$this->initConnect();
}
public function initConnect() {
if ($this->db_pconn) {
$this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
$this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
if ($this->conn) {
$this->query("SET NAMES " . $this->db_charset);
} else {
$this->msg = "数据库连接出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
}
$this->selectDb($this->db_name);
}
public function selectDb($dbname) {
if ($dbname == "") {
$this->db_name = $dbname;
}
if (!mysql_select_db($this->db_name, $this->conn)) {
$this->msg = "数据库不可用";
}
}
public function query($sql, $debug = false) {
if (!$debug) {
$this->result = @mysql_query($sql, $this->conn);
} else {
}
if ($this->result == false) {
$this->msg = "sql执行出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
}
// var_dump($this->result);
}
public function select($tableName, $columnName = "*", $where = "") {
$sql = "SELECT " . $columnName . " FROM " . $tableName;
$sql .= $where ? " WHERE " . $where : null;
$this->query($sql);
}
public function findAll($tableName) {
$sql = "SELECT * FROM $tableName";
$this->query($sql);
}
public function insert($tableName, $column = array()) {
$columnName = "";
$columnValue = "";
foreach ($column as $key => $value) {
$columnName .= $key . ",";
$columnValue .= "'" . $value . "',";
}
$columnName = substr($columnName, 0, strlen($columnName) - 1);
$columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
$sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
$this->query($sql);
if($this->result){
$this->msg = "数据插入成功。新插入的id为:" . mysql_insert_id($this->conn);
}
}
public function update($tableName, $column = array(), $where = "") {
$updateValue = "";
foreach ($column as $key => $value) {
$updateValue .= $key . "='" . $value . "',";
}
$updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
$sql = "UPDATE $tableName SET $updateValue";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "数据更新成功。受影响行数:" . mysql_affected_rows($this->conn);
}
}
public function delete($tableName, $where = ""){
$sql = "DELETE FROM $tableName";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "数据删除成功。受影响行数:" . mysql_affected_rows($this->conn);
}
}
public function fetchArray($result_type = MYSQL_BOTH){
$resultArray = array();
$i = 0;
while($result = mysql_fetch_array($this->result, $result_type)){
$resultArray[$i] = $result;
$i++;
}
return $resultArray;
}
// public function fetchObject(){
// return mysql_fetch_object($this->result);
// }
public function printMessage(){
return $this->msg;
}
public function freeResult(){
@mysql_free_result($this->result);
}
public function __destruct() {
if(!empty($this->result)){
$this->freeResult();
}
mysql_close($this->conn);
}
}
调用代码如下
复制代码 代码如下:
require_once 'mysql_V1.class.php';
require_once 'commonFun.php';
$db = new mysql('localhost', 'root', '', "test");
//select 查
$db->select("user", "*", "username = 'system'");
$result = $db->fetchArray(MYSQL_ASSOC);
print_r($result);
dump($db->printMessage());
//insert 增
//$userInfo = array('username'=>'system', 'password' => md5("system"));
//$db->insert("user", $userInfo);
//dump($db->printMessage());
//update 改
//$userInfo = array('password' => md5("123456"));
//$db->update("user", $userInfo, "id = 2");
//dump($db->printMessage());
//delete 删
//$db->delete("user", "id = 1");
//dump($db->printMessage());
//findAll 查询全部
$db->findAll("user");
$result = $db->fetchArray();
dump($result);
ps,个人比较喜欢tp的dump函数,所以在commonFun.php文件中拷贝了友好打印函数。使用时将其改为print_r()即可。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]