php提取数据库文章,四 文章类 封装对文章的各种操作(插入数据库、从数据库取出等等)-PHP教程,PHP应用...

这是一个PHP类,用于管理文章的增删查改操作。类中包含了对文章的插入、删除、获取总数、获取单篇文章信息以及按作者获取文章等功能。通过与MySQL数据库交互,实现了对文章数据的CRUD操作,并使用了错误处理和调试信息输出。此外,还涉及到了分页对象的使用。
摘要由CSDN通过智能技术生成

//

// +———————————————————————-+

// | 文章类                                                               |

// +———————————————————————-+

// | copyright (c) 2001 netfish software                                  |

// |                                                                      |

// | author: whxbb(whxbb@21cn.com)                                        |

// +———————————————————————-+

//

// $id: whxbb_article.class.php,v 0.1 2001/8/11 22:18:13 yf exp $

//

// 禁止直接访问该页面

if (basename($http_server_vars[php_self]) == "whxbb_article.class.php") {

header("http/1.0 404 not found");

}

/**

* 文章类

* purpose

*  封装对文章的各类操作

*

*

* @author  : whxbb(whxbb@21cn.com)

* @version : 0.1

* @date    :  2001/8/1

*/

class whxbb_article extends whxbb

{

/** 分页对象 */

var $pager;

function article()

{

$this->whxbb();

}

/**

* 文章写入数据库

* @param $title 文章标题

* @param $author 文章作者

* @param $content 文章内容

* @return 操作出错:一个whxbb_error对象 成功:true

* @access public

*/

function insert($title, $author, $content)

{

new whxbb_debug("insert() start");

// 处理传入的参数

whxbb::operatestring(&$title, in);

whxbb::operatestring(&$author, in);

whxbb::operatestring(&$content, in);

$sql = "insert into article(title,author,content) values($title,$author,$content)";

if( !@mysql_query($sql, $this->_conn) )

{

return new whxbb_error("insert() failed.($sql)", 1021);

}

new whxbb_debug("insert() completed");

return true;

}

/**

* 删除指定的记录

* @param $id 要删除记录的id

* @return 操作出错:一个whxbb_error对象 成功:true

* @access public

*/

function del($id)

{

new whxbb_debug("del($id) start");

$sql = "delete from article where id=$id)";

if( !@mysql_query($sql, $this->_conn) )

{

return new whxbb_error("del() failed.($sql)", 1024);

}

new whxbb_debug("dle($id) completed");

return true;

}

/**

* 得到文章的总数

* @param $condition      查询条件

* @return 操作出错:一个whxbb_error对象 成功:true

* @access public

*/

function getcount($condition = )

{

new whxbb_debug("getcount() start");

$sql = "select count(id) from article where  1=1 $condition";

if( !$result = @mysql_query($sql, $this->_conn))

{

return new whxbb_error("getcount() failed.($sql)", 1000);

}

list($count) = @mysql_fetch_array($result);

@mysql_free_result($result);

new whxbb_debug("getcount() completed");

return $count;

}

/**

* 得到某一篇文章的所有字段信息

* @param $id 文章id号

* @return 操作出错:一个whxbb_error对象 成功:返回一个关联数组 找不到信息:返回0

* @access public

*/

function getinfo($id )

{

new whxbb_debug("getinfo($id) start");

$sql = "select  id, title, content, author from article where id=$id";

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

if( !$result)

return new whxbb_error("getinfo($id) failed.($sql)", 1002);

if(@mysql_num_rows($result) == 0)

return 0;

$info = @mysql_fetch_array($result);

while (list($var, $key) = each($info))

{

whxbb::operatestring(&$info[$var], out);

}

reset($info);

@mysql_free_result($result);

new whxbb_debug("getinfo($id) completed");

return $info;

}

/**

* 得到所有author为指定作者名的所有记录

* @param $items 每页显示条数,如果为0则表示取出所有记录

* @param page   当前页码

* @param author 作者名

* @param $orderby 排序方式

* @return 操作出错:一个whxbb_error对象 成功:返回一个数组 找不到信息:返回0

* @access public

*/

function getninfobyauthor($items, $page, $author, $orderby = order by id desc)

{

whxbb::operatestring(&$author, in);

$condition = " and author=$author  ";

$result = $this->getninfo($items, $page, $condition, $orderby);

return $result;

}

}

/**

* 列出所有记录

* @param $items 每页显示条数,如果为0则表示取出所有记录

* @param $page  当前页码

* @param $condition 查询条件

* @param $orderby 排序方式

* @return 操作出错:一个whxbb_error对象 成功:返回一个二维数组 找不到信息:返回0

* @access public

*/

function getninfo($items, $page, $condition = , $orderby = order by id desc)

{

new whxbb_debug("getninfo() start");

$limit = ;

//取记录总数

$infocount = $this->getcount($condition);

if ($infocount == 0)

return 0;

if ($items != 0)

{

// 新建一个分页器

$this->pager = new pager($infocount, $items, $page);

$startpos    = $this->pager->startpos;

$limit = " limit ".$startpos.", ".$items;

}

$sql = "select  id, title, author from article where 1=1 $condition $orderby $limit";

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

if( !$result )

return new whxbb_error("getninfo() failed.($sql)", 1001);

if(@mysql_num_rows($result) == 0)

return 0;

$i = 0;

while ($arr = @mysql_fetch_array($result))

{

while(list($var, $key) = each($arr))

{

whxbb::operatestring(&$arr[$var], out);

}

reset($arr);

$info[$i]            = $arr;

$i++;

}

@mysql_free_result($result);

new whxbb_debug("getninfo() completed");

return $info;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值