各位高手能不能把自己写的精彩的类贴出来,让我们这些初学者学习学习

drzy 2003-10-09 09:43:17
比如连接数据库类,session类,还有听说有封装sql语句的类,很想见识一下
...全文
67 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bonniewater 2003-10-09
  • 打赏
  • 举报
回复
up
tiandiqing 2003-10-09
  • 打赏
  • 举报
回复
<?

/******************************************************************************
*
* PHP PHPLIB
* -------------------------
*
* begin : Friday, Nov 29, 2002
* copyright : (C) 2002 The axing Group
* email : zhang.z.y@163.com
*
*
*  com_lib.php : Zhang.z.y
* 
*******************************************************************************/
mysql_connect("localhost","root","44114");
mysql_select_db("mydatabase") or die(mysql_error());
class CDB
{

var $Host = "localhost"; ## 主机名
var $Database = "mydatabase; ## 数据库名;构造函数可以改变默认数据库名
var $User = "root"; ## 用户名
var $Password = "4414"; ## 密码

var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array(); ## 从数据库中读取的数据
var $Row;

var $Errno = 0;
var $Error = "";

var $Auto_free = 0; ## Set this to 1 for automatic mysql_free_result()
var $Auto_commit = 0; ## set this to 1 to automatically commit results

var $debugmode = 0;

/* function CDB($Host,$Database,$User,$Password)
{## 构造函数,改变$Host,$Database,$User,$Password
if (isset($Host)) $this->Host = $Host;
if (isset($Database)) $this->Database = $Database ;
if (isset($User)) $this->User = $User ;
if (isset($Password)) $this->Password = $Password ;
}*/

function connect()
{## 连接Mysql数据库
if ( 0 == $this->Link_ID )
{
$this->Link_ID=mysql_pconnect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID)
{
$this->halt("Link-ID == false, pconnect failed");
}
if (!mysql_query(sprintf("use %s",$this->Database),$this->Link_ID))
{
$this->halt("cannot use database ".$this->Database);
}
}
}

function query($Query_String)
{## 执行Sql语句
$this->connect();
if ($this->debugmode)
printf("Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID)
{
$this->halt("Invalid SQL: ".$Query_String);
}

return $this->Query_ID;
}

function next_record()
{
$this->Record = mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();

$stat = is_array($this->Record); ## 判断变数型态是否为阵列型态
if (!$stat && $this->Auto_free)
{
mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}

function seek($pos)
{
$status = mysql_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
return;
}

function metadata($table)
{
$count = 0;
$id = 0;
$res = array();

$this->connect();
$id = @mysql_list_fields($this->Database, $table);
if ($id < 0)
{
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$this->halt("Metadata query failed.");
}
$count = mysql_num_fields($id);

for ($i=0; $i<$count; $i++)
{
$res[$i]["table"] = mysql_field_table ($id, $i);
$res[$i]["name"] = mysql_field_name ($id, $i);
$res[$i]["type"] = mysql_field_type ($id, $i);
$res[$i]["len"] = mysql_field_len ($id, $i);
$res[$i]["flags"] = mysql_field_flags ($id, $i);
$res["meta"][$res[$i]["name"]] = $i;
$res["num_fields"]= $count;
}

mysql_free_result($id);
return $res;
}

function affected_rows() {
return mysql_affected_rows($this->Link_ID);
}

function num_rows() {
return mysql_num_rows($this->Query_ID);
}

function num_fields() {
return mysql_num_fields($this->Query_ID);
}

function nf() { ## 取得数据条数
return $this->num_rows();
}

function np() { ## 打印数据条数
print $this->num_rows();
}

function f($Name) { ## 取得字段内容
return $this->Record[$Name];
}

function p($Name) { ## 打印字段内容
print $this->Record[$Name];
}

function halt($msg)
{
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,$this->Error);
die("Session halted.");
}

function gen_rand_string($len)
{## return 随机$len位随机字符串;若$len为空,则取8位随机字符串
if (!isset($len)) $len="8";
$chars = array( 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g'
, 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o'
, 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w'
, 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0');
$max_chars = count($chars) - 1;
srand( (double) microtime()*1000000);
$rand_str = '';
for($i = 0; $i < $len; $i++)
{
$rand_str = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $rand_str . $chars[ran
d(0, $max_chars)];
}
return $rand_str;
}

function check_reg_user($no_username)
{ ## 检验是否用户名重复
$sql = "select * from no_user where no_username = '$no_username'";
$this->query($sql);
if ($this->nf() == 0) return 1;
else return 0;
}

/*
*
*/
}
?>


//=========================================================================
//一个连接数据库的类

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧