数据库操作php,一个数据库操作PHP类

/*

* Author 墨龙

* Time 2010年12月2日 15:50:35

*/

$db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);

class mysql{

private $db_host;

private $db_user;

private $db_password;

private $db_table;

private $db_conn; //数据库连接标识;

private $result; //执行query命令的结果资源标识

private $sql; //sql执行语句

private $pre; //数据库表前缀

private $coding; //数据库编码,GBK,UTF8,gb2312

function __construct($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){

$this->db_host = $db_host;

$this->db_user = $db_user;

$this->db_password = $db_password;

$this->db_table = $db_table;

$this->db_conn = $db_conn;

$this->pre = $pre;

$this->coding = $coding;

$this->connect();

}

function connect(){

$this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("数据库链接错误,请检查数据库链接配置!"));

if(!mysql_select_db($this->db_table,$this->db_conn)){

echo "没有找到数据表:".$this->db_table;

}

mysql_select_db($this->db_table,$this->db_conn);

$this->query("SET NAMES $this->coding");

}

/*执行SQL语句的函数*/

function query($sql){

if(emptyempty($sql)){

$this->show_error("你的sql语句不能为空!");

}else{

$this->sql = $sql;

}

$result = mysql_query($this->sql,$this->db_conn);

return $this->result = $result;

}

/*创建添加新的数据库*/

public function create_database($database_name){

$database=$database_name;

$sqlDatabase = 'create database '.$database;

return $this->query($sqlDatabase);

}

// 根据select查询结果计算结果集条数

public function db_num_rows(){

if($this->result==null){

if($this->show_error){

$this->show_error("sql语句错误!");

}

}else{

return mysql_num_rows($this->result);

}

}

/*查询服务器所有数据库*/

//将系统数据库与用户数据库分开,更直观的显示?

public function show_databases(){

$this->query("show databases");

echo "现有数据库:".$amount =$this->db_num_rows($rs);

echo "
";

$i=1;

while($row = $this->fetch_array($rs)){

echo "$i $row[Database]";

echo "
";

$i++;

}

}

//以数组形式返回主机中所有数据库名

public function databases()

{

$rsPtr=mysql_list_dbs($this->db_conn);

$i=0;

$cnt=mysql_num_rows($rsPtr);

while($i

{

$rs[]=mysql_db_name($rsPtr,$i);

$i++;

}

return print_r($rs);

}

/*查询数据库下所有的表*/

function show_tables($database_name){

$this->query("show tables");

echo "现有数据库:".$amount = $this->db_num_rows($rs);

echo "
";

$i=1;

while($row = $this->fetch_array($rs)){

$columnName="Tables_in_".$database_name;

echo "$i $row[$columnName]";

echo "
";

$i++;

}

}

/*

mysql_fetch_row() array $row[0],$row[1],$row[2]

mysql_fetch_array() array $row[0] 或 $row[id]

mysql_fetch_assoc() array 用$row->content 字段大小写敏感

mysql_fetch_object() object 用$row[id],$row[content] 字段大小写敏感

*/

/*取得记录集,获取数组-索引和关联,使用$row['content'] */

public function fetch_array()

{

return @mysql_fetch_array($this->result);

}

//获取关联数组,使用$row['字段名']

public function fetch_ass()

{

return @mysql_fetch_assoc($this->result);

}

//获取数字索引数组,使用$row[0],$row[1],$row[2]

public function fetch_row()

{

return @mysql_fetch_row($this->result);

}

//获取对象数组,使用$row->content

public function fetch_Object()

{

return @mysql_fetch_object($this->result);

}

//简化查询select

public function findall($table){

$table = $this->fulltablename($table);

$this->query("select * from $table");

}

public function select($table,$columnName,$condition){

$table = $this->fulltablename($table);

if(emptyempty($columnName)){

$columnName = "*";

}

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

}

//简化的insert

function insert($table,$arr){

$table = $this->fulltablename($table);

$sql = "INSERT INTO $table ";

if(!is_array($arr)){

$this->show_error("请输入参数数组!");

}else{

$k = "";

$v = "";

foreach($arr as $key => $value){

$k .= "`$key`,";

$v .= "'".$value."',";

}

}

$sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";

$this->query($sql);

}

//简化的update

function update($table,$arr,$where){

$table = $this->fulltablename($table);

$sql = "UPDATE $table SET ";

if(!is_array($arr)){

$this->show_error("请输入参数数组!");

}else{

foreach($arr as $key => $value){

$sql .= " `".$key."` = '".$value."' ,";

}

}

$sql = substr($sql,0,-1)." where ".$where;

return $this->query($sql);

}

//简化的delete

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

$table = $this->fulltablename($table);

if(emptyempty($where)){

$this->show_error("条件不能为空!");

}else{

$where = " where ".$where;

}

$sql = "DELETE FROM $table ".$where;

//echo $sql;

return $this->query($sql);

}

//取得上一步 INSERT 操作产生的 ID

public function insert_id(){

return mysql_insert_id();

}

//加上前缀的数据表

public function fulltablename($table){

return $table = $this->pre.$table;

}

//查询字段数量

public function num_fields($table){

$table = $this->fulltablename($table);

$this->query("select * from $table");

echo "
";

echo "字段数:".$total = mysql_num_fields($this->result);

echo "

";

for ($i=0; $i

print_r(mysql_fetch_field($this->result,$i) );

}

echo "

";

echo "
";

}

//取得 MySQL 服务器信息

public function mysql_server($num=''){

switch ($num){

case 1 :

return mysql_get_server_info(); //MySQL 服务器信息

break;

case 2 :

return mysql_get_host_info(); //取得 MySQL 主机信息

break;

case 3 :

return mysql_get_client_info(); //取得 MySQL 客户端信息

break;

case 4 :

return mysql_get_proto_info(); //取得 MySQL 协议信息

break;

default:

return mysql_get_client_info(); //默认取得mysql版本信息

}

}

//析构函数,自动关闭数据库,垃圾回收机制

/*public function __destruct()

{

if(!empty($this->result)){

$this->free();

}

mysql_close($this->$db_conn);

}*/

/*获得客户端真实的IP地址*/

function getip(){

if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))

{

$ip = getenv("HTTP_CLIENT_IP");

}

else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){

$ip = getenv("HTTP_X_FORWARDED_FOR");

}

else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))

{

$ip = getenv("REMOTE_ADDR");

}

else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){

$ip = $_SERVER['REMOTE_ADDR'];

}

else{

$ip = "unknown";

}

return($ip);

}

function show_error($str){

echo "";

}

}

?>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/567911.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STL18常用算法

#include<iostream> #include<algorithm> #include<vector> using namespace std; //transform 将一个容器中的元素搬运在另一个容器中 #if 0 //错误 struct PrintVector {void operator()(int v) {cout << v << " ";} }; void test0…

eclipse java ui,Eclipse Forms筹划漂亮UI之高级组件[Java编程]

赞助商链接本文“Eclipse Forms筹划漂亮UI之高级组件[Java编程]”是由七道奇为您精心收集&#xff0c;来源于网络转载&#xff0c;文章版权归文章作者所有&#xff0c;本站不对其观点以及内容做任何评价&#xff0c;请读者自行判断&#xff0c;以下是其具体内容&#xff1a;Ecl…

php中页面平滑回到顶部代码,原生JS实现平滑回到顶部组件

返回顶部组件是一种极其常见的网页功能&#xff0c;需求简单&#xff1a;页面滚动一定距离后&#xff0c;显示返回顶部的按钮&#xff0c;点击该按钮可以将滚动条滚回至页面开始的位置。实现思路也很容易&#xff0c;只要改变document.documentElement.scrollTop或document.bod…

C++基础01-C++对c的增强

所谓namespace&#xff0c;是指标识符的各种可见范围。C标准程序库中的所 有标识符都被定义于一个名为std的namespace中。 一 &#xff1a;<iostream>和<iostream.h>格式不一样&#xff0c;前者没有后缀&#xff0c;实际上&#xff0c; 在你的编译器include文件夹…

php环行队列实现,java数组实现队列及环形队列实现过程解析

这篇文章主要介绍了java数组实现队列及环形队列实现过程解析,文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下代码内容ArrayQueue---用数组实现队列package com.structure;import java.util.Scanner;/*** auther:…

C++混淆点-static关键字理解

.先来介绍它的第一条也是最重要的一条&#xff1a;隐藏。&#xff08;static函数&#xff0c;static变量均可&#xff09; 当同时编译多个文件时&#xff0c;所有未加static前缀的全局变量和函数都具有全局可见性。 举例来说明。同时编译两个源文件&#xff0c;一个是a.c&…

php分页代码 页数太多,php分页函数示例代码分享

一例php分页函数代码。分享一例php分页函数代码&#xff0c;用此函数实现分页代码很不错。代码&#xff0c;php分页函数。/** 使用方法&#xff1a;require_once(mypage.php);$resultmysql_query("select * from mytable", $myconn);$totalmysql_num_rows($result); …

C++基础02-C++对c的拓展

变量名实质上是一段连续存储空间的别名&#xff0c;是一个标号(门牌号) 通过变量来申请并命名内存空间. 通过变量的名字可以使用存储空间. 变量名&#xff0c;本身是一段内存的引用&#xff0c;即别名(alias). 引用可以看作一个已定义变量的别名。 引用的语法&#xff…

matlab在电力系统故障的应用,MATLAB在电力系统故障分析中的应用

第5章MATLAB在电力系统故障分析中的仿真实例 5 1无穷大功率电源供电系统三相短路仿真5 2同步发电机突然短路的暂态过程仿真5 3小电流接地系统单相故障 5 1无穷大功率电源供电系统三相短路仿真 5 1 1无穷大功率电源供电系统三相短路的暂态过程5 1 2无穷大功率电源供电系统仿真模…

C++基础03-C++对c的拓展-函数

一、内联函数 c 语言中有宏函数的概念。宏函数的特点是内嵌到调用代码中去,避免了函数调用 的开销。但是由于宏函数的处理发生在预处理阶段,缺失了语法检测 和有可能带来的语 意差错。 特点&#xff1a; 1&#xff09;内联函数声明时inline关键字必须和函数定义结合在一起&a…

php小程序onload,微信小程序 loading 组件实例详解

这篇文章主要介绍了微信小程序 loading 组件实例详解的相关资料,需要的朋友可以参考下loading通常使用在请求网络数据时的一种方式&#xff0c;通过hidden属性设置显示与否主要属性&#xff1a;wxml显示loading正在加载jsPage({data:{// text:"这是一个页面"hiddenLo…

机器学习之实验过程02

机器学习之实验过程-数据清理 from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_errordata_path = /home/py/Work/机器学习/labs/data/Feedback.csv df = pd.read_csv(data_path) df.head() print (df.tail()) rename_pai…

C++基础04-类基础

一、类和对象 面向对象三大特点&#xff1a;封装、继承、多态。 struct 中所有行为和属性都是 public 的(默认)。C中的 class 可以指定行为和属性的访问方式。 封装,可以达到,对内开放数据,对外屏蔽数据,对外提供接口。达到了信息隐蔽的功能。 class 封装的本质,在于将数…

matlab 矩阵位移法编程 结构力学,matlab 矩阵位移法编程 结构力学

矩阵位移法编程大作业(091210211)一、编制原理本程序的原理是基于结构力学矩阵位移法原理&#xff0c;以结构结点位移作基本未知量&#xff0c;将要分析的结构拆成已知节点力—结点力位移关系的单跨梁集合&#xff0c;通过强令结构发生待定的基本未知位移&#xff0c;在各个单跨…

C++混淆点-构造函数参数

#include<iostream> using namespace std;class Test { public://Test(int x, int y) { //或者将形参名不要等于数据成员名// //x x; //自赋值 错误// //y y;// this->x x; //正确 形参a屏蔽了成员变量a&#xff0c;所以必须用this指针索引&#xff0c;这样程序直…

php 静态页面模板类,dedetag.class.php 静态模板类

类文件include/dedetag.class.php这个文件是dedecms V5.3及之前版本使用的主要的模板类&#xff0c;它是解析式模板类&#xff0c;并非编译式的(区别是前者通过获得标签位置进行内容替换&#xff0c;后者是直接解析式PHP代码&#xff0c;二次执行)一、模板语法织梦模板引擎是一…

C/C++混淆点-字符串指针

c语言中没有字符串的类型&#xff0c; 所以对字符串操作&#xff0c;有两种形式:可以用字符指针&#xff0c;或者字符串数组&#xff08;这里的指针变量c, 系统会为其重新分配内存) &#xff08;1&#xff09;用字符数组存放一个字符串 char string[]"Linux C"; pri…

python 文件上传下载,python实现上传下载文件功能

最近刚学python,遇到上传下载文件功能需求&#xff0c;记录下&#xff01;django web项目&#xff0c;前端上传控件用的是uploadify。文件上传 - 后台view 的 Python代码如下&#xff1a;csrf_exemptrequire_http_methods(["POST"])def uploadFiles(request):try:use…

C++基础05-类构造函数与析构函数

总结&#xff1a; 1、类对象的作用域为两个{}之间。在遇到}后开始执行析构函数 2、当没有任何显式的构造函数&#xff08;无参&#xff0c;有参&#xff0c;拷贝构造&#xff09;时&#xff0c;默认构造函数才会发挥作用 一旦提供显式的构造函数&#xff0c;默认构造函数不复…

PHP网站配置项,Thinkphp5通用网站后台配置项的动态添加及更新

一、引入无论平时我们自己制作&#xff0c;还是浏览别人的网站&#xff0c;它都具有其相应的一些共用的、通用的属性&#xff0c;比如&#xff1a;网站的名字&#xff0c;关键字、备案号、分页数量、是否开启缓存等信息。一些网站可能将配置项写死在后台&#xff0c;无法动态更…