仓酷云

标题: PHP教程之Mysql数据库操作类( 1127版,供应源码下... [打印本页]

作者: 再见西城    时间: 2015-2-3 23:41
标题: PHP教程之Mysql数据库操作类( 1127版,供应源码下...
可以说你的马步已经扎的差不多了,接下来就要开始练把势的时候了,如果有条件的话,用笔或者打印一个简易的PHP手册在身上,时不时的摸出来看看,记得,去WC也不能放过(^2^)。   Mysql.class.php 下载
复制代码 代码以下:
<?php
class Mysql {
private $db_host; //主机地址
private $db_user; //用户名
private $db_pass; //毗连暗码
private $db_name; //称号
private $db_charset; //编码
private $conn;
public $debug=false;//调试开关,默许封闭
private $query_id; //用于判别sql语句是不是履行胜利
private $result; //了局集
private $num_rows; //了局集中行的数量,仅对select无效
private $insert_id; //上一步 INSERT 操作发生的 ID
// 机关/析构函数
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 毗连/选择数据库
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('数据库-毗连掉败:用户名或暗码毛病!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("数据库-选择掉败:数据库 $this->db_name 不成用");
}
mysql_query("SET NAMES $this->db_charset");
return $this->conn;
}
// query办法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("SQL语句 <b>\"$sql\"</b> 履行时碰到毛病");
return $this->query_id;
}
// 显示具体毛病信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "毛病:$msg <br/> 前往:$errinfo<p>";
}else{
echo '<p>呈现毛病!<p>';
}
}
// 取得query履行胜利与否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查询一切
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 获得 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 获得 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 显示共有几何张表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}
// 显示共有几何个数据库
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共无数据库 ".$this->num_rows($this->query_id)." 个<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[Database]."<br />";
$i ++;
}
}
}
// 删除数据库:前往删除了局
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默许删除以后数据库
$this->query("DROP DATABASE $db_name");
}else {
$this->query("DROP DATABASE $db_name");
}
if ($this->query_id) {
return "数据库 $db_name 删除胜利";
}else {
$this->show_error("数据库 $db_name 删除掉败");
}
}
// 删除数据表:前往删除了局
public function drop_table ($table_name) {
$this->query("DROP TABLE $table_name");
if ($this->query_id) {
return "数据表 $table_name 删除胜利";
}else {
$this->show_error("数据表 $table_name 删除掉败");
}
}
// 创立数据库
public function create_db ($db_name) {
$this->query("CREATE DATABASE $db_name");
if($this->query_id){
return "数据库 $db_name 创立胜利";
}else {
$this->show_error("数据库 $db_name 创立掉败");
}
}
// 获得数据库版本
public function get_info(){
echo mysql_get_server_info();
}
// 释放内存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // End class
?>

当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与,
作者: 金色的骷髅    时间: 2015-2-4 06:00
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
作者: 柔情似水    时间: 2015-2-6 08:27
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
作者: 因胸联盟    时间: 2015-2-6 19:40
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
作者: 海妖    时间: 2015-2-18 11:09
做为1门年轻的语言,php一直很努力。
作者: 第二个灵魂    时间: 2015-2-27 15:23
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
作者: 仓酷云    时间: 2015-3-6 00:08
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
作者: 飘飘悠悠    时间: 2015-3-9 07:50
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
作者: 灵魂腐蚀    时间: 2015-3-16 21:04
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
作者: 活着的死人    时间: 2015-3-17 07:11
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
作者: 冷月葬花魂    时间: 2015-3-17 20:09
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
作者: 小妖女    时间: 2015-3-19 18:02
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
作者: 小女巫    时间: 2015-3-24 19:53
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
作者: 乐观    时间: 2015-4-1 02:11
曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线  \\\\\\\'_\\\\\\\' ;
作者: 变相怪杰    时间: 2015-4-3 16:25
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
作者: 愤怒的大鸟    时间: 2015-4-17 08:57
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
作者: 精灵巫婆    时间: 2015-4-18 09:41
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
作者: 只想知道    时间: 2015-4-22 10:07
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
作者: 若天明    时间: 2015-4-26 06:11
首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2