≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php 操作MySql数据库类的简单示例

人气:645 时间:2018-09-20

这篇文章主要为大家详细介绍了php 操作MySql数据库类的简单示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
经测试代码如下:

<?php
/**
 * 操作MySql数据库类
 *
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/
class DB{
var $host;
var $user_name;
var $password;
var $db_name;
var $link_id;
var $result;
var $col;
var $query;
var $fields;
var $records;
var $setting;
var $debug = false;
var $query_count = 0;
var $debug_file = "debug.sql";
function settings($key,$value){
$this->setting[$key] = $value;
}
function init($_host, $_user, $_password, $_db_name){
$this->host = $_host;
$this->user_name = $_user;
$this->password = $_password;
$this->db_name = $_db_name;
$this->fields = array();
$this->link_id = @mysql_connect($_host, $_user, $_password) or die("Your website is not properly installed.");
@mysql_select_db($_db_name, $this->link_id);
}
function assign($field, $value){
$this->fields[$field] = ($value)==""?("'".$value."'"):$value;
}
function assign_str($field, $value){
$this->fields[$field] = "'".addslashes($value)."'";
//$this->fields[$field] = "'".($value)."'";
}
function reset(){
$this->fields = array();
}
function insert($table){
$f = "";
$v = "";
reset($this->fields);
foreach($this->fields as $field=>$value){
$f.= ($f!=""?", ":"").$field;
$v.= ($v!=""?", ":"").$value;
}
$sql = "INSERT INTO ".$table." (".$f.") VALUES (".$v.")";
$this->query($sql);
return $this->insert_id();
}
function update($table, $where){
$f = "";
reset($this->fields);
foreach($this->fields as $field=>$value){
$f.= ($f!=""?", ":"").$field." = ".$value;
}
$sql = "UPDATE ".$table." SET ".$f." ".$where;
$this->query($sql);
}
function timestampFormat($unixNumber){
return date('Y-m-d H:i:s',$unixNumber);
///      xxxx-xx-xx xx-xx-xx
}
function query($_query){
list($usec, $sec) = explode(" ",microtime());
$time_start  = ((float)$usec + (float)$sec);
$this->query = $_query;
$this->result = @mysql_query($_query, $this->link_id) or die( $_query."<p>".mysql_error($this->link_id) );
list($usec, $sec) = explode(" ",microtime());
$time_end  =  ((float)$usec + (float)$sec);
$time = $time_end - $time_start;
if($this->debug){
$this->query_count ++;
$f = fopen($this->debug_file, "a");
$sss = "# ".$this->query_count."\n ".$time." sec \n\n".$_query."\n#-------------------------------------------------------------------------\n\n";
fputs($f, $sss, strlen($sss));
fclose($f);
}
return $this->result;
}
function get_records(){
$this->records = array();
while($row = @mysql_fetch_array($this->result, MYSQL_BOTH)){
$this->records[count($this->records)] = $row;
}
reset($this->records);
return $this->records;
}
function get_tables_status(){
$this->query("SHOW TABLE STATUS FROM `".$this->db_name."`");
if($this->num_rows() > 0){
$tables = array();
while($this->movenext()){
$tables[$this->col["Name"]] = $this->col;
}
return $tables;
}
return false;
}
function fetch_array(){
$this->col = @mysql_fetch_array($this->result, MYSQL_BOTH);
}
function num_rows(){
return (int)@mysql_num_rows($this->result);
}
function fixSlashes(){
if($this->col){
foreach($this->col as $key => $value)
$this->col[$key] = stripslashes($value);
return $this->col;
}
}
function movenext(){
$this->col=@mysql_fetch_array($this->result, MYSQL_ASSOC);
if($this->setting['fixSlashes'])
return $this->fixSlashes();
else
return $this->col;
}
function done(){
@mysql_close($this->link_id);
}
function insert_id(){
return @mysql_insert_id($this->link_id);
}
function affected_rows(){
return @mysql_affected_rows($this->link_id);
}
}



/*** 来自:四海网(www.q1010.com) ***/ 
?>

本文来自:http://www.q1010.com/173/549-0.html

注:关于php 操作MySql数据库类的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:操作数据库

您可能感兴趣的文章

  • php 操作mysql数据库的类入门实例
  • php 操作mysql数据库的封装类功能实例
上一篇:php 生成zip文件的类的简单示例
下一篇:php 一个超强分页类的完整代码
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。