Table of Contents
php爱好者留言簿 提示信息
Home Backend Development PHP Tutorial 从一个不错的留言本弄的mysql数据库操作类_php技巧

从一个不错的留言本弄的mysql数据库操作类_php技巧

May 17, 2016 am 09:39 AM

从一个不错的留言本弄的mysql数据库操作类,初学php的朋友可以参考下

复制代码 代码如下:

class mysql{
    var $querynum = 0;
    function connect($dbhost, $dbuser, $dbpw, $dbname = '',$dbcharset='') {
        if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
            $this->show('Can not connect to MySQL server');
            return false;
        }
        if($dbname) {
            $this->select_db($dbname);
        }
        if($this->version() > '4.1' && $dbcharset) {
            $this->query("SET NAMES '".$dbcharset."'");
        }
        return true;
    }

    function select_db($dbname) {
        return mysql_select_db($dbname);
    }

    function fetch_array($query, $result_type = MYSQL_ASSOC) {
        return @mysql_fetch_array($query, $result_type);
    }

    function query($sql, $type = '') {
        if(!($query = mysql_query($sql))) $this->show('MySQL Query Error', $sql);
        $this->querynum++;
        return $query;
    }

    function affected_rows() {
        return mysql_affected_rows();
    }

    function result($query, $row) {
        return mysql_result($query, $row);
    }

    function num_rows($query) {
        return @mysql_num_rows($query);
    }

    function num_fields($query) {
        return mysql_num_fields($query);
    }

    function free_result($query) {
        return mysql_free_result($query);
    }

    function insert_id() {
        return mysql_insert_id();
    }

    function fetch_row($query) {
        return mysql_fetch_row($query);
    }

    function version() {
        return mysql_get_server_info();
    }

    function close() {
        return mysql_close();
    }

    function error() {
        return mysql_error();
    }

    function show($message = '', $sql = '') {
        if(!$sql) echo $message;
        else echo $message.'
'.$sql.'
'.$this->error();
    }
}

class page extends mysql{
    function pagination($sql,$maxnum,$page,$maxpages,$pagepre,$ext=''){
        global $sum,$stail,$link,$lmid,$ltail,$curpage;//$ext='&class=3'
        $SELF = $_SERVER['PHP_SELF'];

        $query = $this->query($sql);
        $rows = $this->fetch_array($query,MYSQL_NUM);
        $totalrows = $rows[0];

        $totalpages = ceil($totalrows/$maxnum);
        $startnum = ($page - 1)*$maxnum;
        $string = $sum.$totalrows.$stail.$sum.$page."/".$totalpages.$stail;

        if($page != 1){
            $string .= $link.$SELF."?page=1".$ext.$lmid."|‹".$ltail;
            $string .=  $link.$SELF.'?page='.($page - 1).$ext.$lmid."‹‹".$ltail;
        }

        if($maxpages>=$totalpages){
            $pgstart = 1;$pgend = $totalpages;
        }
        elseif(($page-$pagepre-1+$maxpages)>$totalpages){
            $pgstart = $totalpages - $maxpages + 1;
            $pgend = $totalpages;
        }
        else{
            $pgstart=(($page            $pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
        }

        for($pg=$pgstart;$pg            if($pg == $page){
                $string .=  $curpage.$SELF."?page=".$pg.$ext.$lmid.$pg.$ltail;
            }
            else $string .=  $link.$SELF."?page=".$pg.$ext.$lmid.$pg.$ltail;
        }

        if($page != $totalpages){
            $string .=  $link.$SELF.'?page='.($page + 1).$ext.$lmid."››".$ltail;
            $string .=  $link.$SELF.'?page='.$totalpages.$ext.$lmid."›|".$ltail;
        }
        return $string;
    }
}

function html($str,$allowhtml=0){
    $str = get_magic_quotes_gpc()?$str:addslashes($str);
    if(!$allowhtml){$str = htmlspecialchars($str);}
    return $str;
}

function dehtml($str,$allowhtml=0){
    $str = stripslashes($str);
    if(!$allowhtml){$str = nl2br($str);}
    return $str;
}

function deip($str){
    $arr = explode('.',$str);
    $str = $arr[0].'.'.$arr[1].'.'.$arr[2].'.*';
    return $str;
}

function setting($login=false){
    global $db,$mydbpre;
    if($login) $sql = "select * from {$mydbpre}setting";
    else $sql = "select * from {$mydbpre}setting where keyword != 'username' and keyword != 'password'";
    $query = $db->query($sql);
    while($row = $db->fetch_array($query)){
        $arr[$row['keyword']] = $row['val'];
        if($row['keyword'] == 'name') $arr['title'] = $row['val'].' - Powered by PHPfans';
    }
    return $arr;
}

function checkcontent($content){
    global $db,$mydbpre,$mearr;
    $arrword = $arrip = array();
    $sql = "select * from {$mydbpre}ban where b_type != 0";
    $query = $db->query($sql);
    while($row = $db->fetch_array($query)){
        if($row['b_type'] == 1) $arrword[] = $row['b_val'];
        elseif($row['b_type'] == 2) $arrip[] = $row['b_reval'];
    }
    foreach($arrip as $value){
        $value = preg_quote($value,'/');
        $value = str_replace('\*','\d{1,3}',$value);
        if(preg_match('/^('.$value.')$/',$_SERVER['REMOTE_ADDR'])){
            showmessage($mearr[8],'index.php');
        }
    }
    foreach($arrword as $val){
        $val = preg_quote($val,'/');
        if(preg_match('/'.$val.'/',$content)){
            showmessage($mearr[7],'index.php');
        }
    }
}

function replaceword($content){
    global $db,$mydbpre;
    $sql = "select * from {$mydbpre}ban where b_type = 0";
    $query = $db->query($sql);
    while($row = $db->fetch_array($query)){
        $content = str_replace($row['b_val'],$row['b_reval'],$content);
    }
    return $content;
}

function showadmin($message,$referer=''){
    $str = '';
    if($referer != '') $str .= '';
    $str .= '
  
  
    
  
  
    
  
  
信息提示
'.$message;
    if($referer != '') $str .= '

  

  如果你的页面没有跳转,请点这里';

    $str .= '
  
';
    echo $str;
    exit;
}

function showmessage($message,$referer=''){
    global $setting,$db;
    $start = gettime();
    echo nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">



php爱好者留言簿_提示信息

EOT;
    if($referer != '') echo '';
    echo 


EOT;
    require_once('header.html');
    echo 

php爱好者留言簿 提示信息


{$message}
EOT;
    if($referer != ''){
        echo 

如果您的浏览器没有自动跳转,请点击这里

EOT;
}
require_once('footer.html');
echo 



EOT;
exit;
}

function gettime()
{
    $t = explode(" ",microtime());
    return $t[1] + $t[0];
}

function checklogin($exit=true){
    if(!isset($_COOKIE['islogin']) || $_COOKIE['islogin'] != 1){
        if($exit) {
            echo "请先登陆,谢谢。";
            exit;
        }
    }
    else setcookie('islogin',1,time()+60*20);
}
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles