首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

一个可以针对本土数据库的新闻搜索,添加新闻和单词库操作的php程序

2013-09-29 
一个可以针对本地数据库的新闻搜索,添加新闻和单词库操作的php程序程序运行环境:Apachemysqlphp5phpMyAdmi

一个可以针对本地数据库的新闻搜索,添加新闻和单词库操作的php程序

程序运行环境:Apache+mysql+php5+phpMyAdmin。在本地数据库要事先新建好一个数据表以供调用。这个操作可以在数据库操作软件phpMyAdmin中进行。然后就是写程序,先写html文件,就是一个表单文件,提交给pwd.php文件进行处理,pwd.php文件调用def.class.php文件来进行数据库的添加、删除、更改、查询操作,在def.class.php文件中返回搜索结果就是用echo 语句就可以实现,当然这里面有几点要注意,由于添加新闻到数据库中的同时也必须生成一个新闻页面文件,以供搜索结果产生的本地文章链接去调用,要不然URL就没有指定的文件与它对应,为每个新闻建立的页面其实都是根据模板文件建立的,具体就是temp.php中的model.htm文件,这个文件就是一个模板文件,如果懂做网站的人就知道其实模板文件也是一个页面,只不过它有很多标签供数据库调用去替换相应的表签得到的具体页面。

界面文件:access.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>www.codeke.com</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <?php    //header("content-type:text/html;charset=utf-8");     class SqlDiy{    private $time;    private $dbtime;    private $pwd;    private $danci; //symbol for the field named title and danci    private $user;    private $jiudanci;  //symbol for the field named jiudanci and newsText    private $oper;    private $url;    function __construct($user,$pwd){    $this->user=$user;    $this->pwd=$pwd;    $this->timeWorkErr();    //$this->createDataBase();    }    function timeWorkErr(){    $this->time=time();    date_default_timezone_set('PRC');    }    function createDataBase(){ //建立一个数据表    $link=mysql_connect('localhost',"root","zhulike");//必须用root账号和密码登录    if(!$link){    die('Could not connect: ' . mysql_error());    }      $sql = 'DROP DATABASE discuz';    if(mysql_query($sql, $link)) {    echo "Database my_db created successfully\n";    } else {    echo 'Error creating database: ' . mysql_error() . "\n";    }            mysql_close($link);     }    function connSql(){        $conn=mysql_connect("localhost",$this->user,$this->pwd);    if(!$conn){    echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    mysql_query("set names utf8");    switch($this->oper){    case "select":$query="select * from `danci` where `danci`='$this->danci'";break;     case "delete":$query="delete from `danci` where danci='$this->danci'";break;    case "insert":$query="insert into `danci`(danci)values('$this->danci')";break;    case "update":$query="update `danci` set `danci`='$this->danci' where `danci`='$this->jiudanci'";break;    }    //$query="update user set name='lily' where name='lily'";    mysql_query("select * from `danci` where danci='$this->danci'",$conn);            if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){    echo "<br />单词库中已经有该单词,无需添加";     return ;    }      if(mysql_affected_rows($conn)<1&&$this->oper=="select"){    echo "<br />单词库里没有该单词"; //针对查询操作    return ;    }    if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){    echo "<br />无法删除一个没有的单词"; //针对删除操作    return ;    }    $res=mysql_query($query,$conn);    if(mysql_affected_rows($conn)<1){    echo "<br />连接数据库有误$this->jiudanci--$this->danci";    error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    else {    if($this->oper=="select"){           while($rows=mysql_fetch_object($res)){     echo "<br />".$rows->id."--".$rows->danci."--".$rows->dt;    }    mysql_free_result($res);    }    if($this->oper=="insert"){    echo "<br />插入单词成功";    }    if($this->oper=="delete"){    echo "<br />删除单词成功";    }    if($this->oper=="update"){    echo "<br />更新单词成功";    }    }        mysql_close($conn);    }    function connNews(){        $conn=mysql_connect("localhost",$this->user,$this->pwd);    if(!$conn){    echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    mysql_query("set names utf8");    switch($this->oper){    case "select":$query="select * from `news` where title like '%$this->danci%'";break;    case "delete":$query="delete from `news` where title='$this->danci'";break;    case "insert":$query="insert into `news`(title,newsText,url)values('$this->danci','$this->jiudanci','$this->url')";break;    }        mysql_query("select title from `news` where title='$this->danci'",$conn);    if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){    echo "<br />已经有该新闻,无需添加";    return ;    }        if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){    echo "<br />无法删除一个没有的新闻"; //针对删除操作    return ;    }    $res=mysql_query($query,$conn);      if(mysql_affected_rows($conn)<1){    echo "<br />连接数据库有误";    error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");    }    else {    if($this->oper=="select"){    $tempStr="<h2>搜索结果:</h2><br /><ul><br />";    while($rows=mysql_fetch_object($res)){        $tempStr .= "<br /><li><a href='$rows->url'>$rows->title</a>发布时间:$rows->dt</li><br />$rows->newsText<br /><br />";    }    $tempStr.="</ul>";echo "$tempStr";    //setdbHtml($tempStr,"test for win32");    mysql_free_result($res);    }    else{    $query="select dt from `news` where title='$this->danci' limit 1";    $res=mysql_query($query,$conn);    $rows=mysql_fetch_object($res);    $this->dbtime=$rows->dt;    if($this->oper=="insert"){    echo "<br />添加新闻成功";    }    if($this->oper=="delete"){    echo "<br />删除新闻成功";    }    }          }        mysql_close($conn);    }    function getTime(){    return $this->dbtime;    }    function setJiudanci($i){    $this->jiudanci=$i;    }    function setOper($i){    $this->oper=$i;    }    function setDanci($i){    $this->danci=$i;    }    function setUrl($i){            $this->url=$i;      }    }    ?>

其实先理清这些文件的调用过程就是所谓的框架写代码就是最简单的,不过我还是太低级了!还没有搞懂用框架去写代码,我想就像写作文一样,事先先列一个怎么写的提纲在去写作文就会好多了,把每一部分的结构每一个文件的作用弄清楚是最好不过了。



热点排行