2016年3月14日 星期一

pear的資料庫處理

新增.修改.刪除.瀏覽,資料庫四大功能.
一. 新增
            $table_name = 'goodurl_cat';
            $table_fields = array('category_title','rank','username');
            $datas = array(stripslashes($_POST['category_title']),$_POST['rank'],$authUsername);
            $pre = $db->autoPrepare($table_name,$table_fields,DB_AUTOQUERY_INSERT);
            $db->execute($pre,$datas);
二. 修改
            $table_name = 'goodurl_cat';
            $table_fields = array('category_title','rank');
            $datas = array(stripslashes($_POST['category_title']),$_POST['rank']);
            $pre = $db->autoPrepare($table_name,$table_fields,DB_AUTOQUERY_UPDATE,"category_id=".$_POST['category_id']);
            $db->execute($pre,$datas);
三. 刪除
            $db->query(sprintf("DELETE FROM goodurl_cat WHERE category_id in (%s)",implode(",",$_POST['cids'])));
四. 瀏覽
        $strSearch = trim($_GET['search']);
        $strWhere = ($strSearch == '') ? '' : " and (locate('$strSearch',category_title)>0 or locate('$strSearch',name)>0)";
        if (!$isAll) $strWhere .= " and goodurl_cat.username='".$authUsername."'";
        $query = "select goodurl_cat.*,name from goodurl_cat join user on goodurl_cat.username=user.username where category_id>0 $strWhere order by rank ASC,category_id DESC";
        $rows = $db->getAll($query);