眉若轻烟的BLOG
个人信息
眉若轻烟
日历
最新文章
访客
相册
文章分类
计数器
          
rss
 
推荐订阅:订阅到RSS阅读
内容
  •  
    2008-05-05 10:03:20
     

    MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法,使得原本需要执行3条SQL语句(SELECT,INSERT,UPDATE),缩减为1条语句即可完成。

    例如ipstats表结构如下:

     CREATE TABLE ipstats ( ip VARCHAR(15) NOT NULL UNIQUE, clicks SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0' ); 
    

    原本需要执行3条SQL语句,如下:

     IF (SELECT * FROM ipstats WHERE ip='192.168.0.1') {     UPDATE ipstats SET clicks=clicks+1 WHERE ip='192.16
    
  •  
    2008-04-09 22:53:49
    标签:php 杂谈
     

    array_merge_recursive

    (PHP 4 >= 4.0.1)

    array_merge_recursive -- 递归地合并两个或多个数组

    说明

    array array_merge_recursive ( array array1, array array2 [, array ...])

    array_merge_recursive() 将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。

    如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。

    例子 1. array_merge_recursive() 例子

    <?php
    $ar1
    = array ("color" => array ("favorite" => "red"), 5);
    $ar2 = array (10, "color" => array ("favorite" => "green", "blue"));
    $result
  •  
    2008-04-09 22:48:05
    标签:php usort 杂谈
     usort --  使用用户自定义的比较函数对数组中的值进行排序

    说明

    void usort ( array array, callback cmp_function)
     

    usort的回调函数cmd接受两个参数,这两个参数由usort内部提供
    回调函数可能的返回值是
    0 不做操作
    -1 将$a所在单元先前移动
    1 将$b所在单元先前移动

    也就是说当$a>$b时,返回1,就是做升序排列;返回-1,就是做降序排列


    <?php
    function cmp ($a, $b) {
        if (
    $a == $b) return 0;
        return (
    $a < $b) ? -1 : 1;
    }

    $a = array (3, 2, 5, 6, 1);

    usort ($a,
  •  
    2008-04-02 10:10:03
    标签:mysql 杂谈
    Query Error: SELECT * FROM pw_threads WHERE tid IN(44537,60633,86761,86741,100709,104140,384) AND topped>1 ORDER BY topped DESC,lastpost DESC LIMIT 0,7
    The URL Is:
    http://bbs.windy8.com/thread-htm-fid-108.html?fid-108.html
    MySQL Server Error:
    Can’t open file: ‘pw_threads.MYI’ (errno: 145) ( 1016 )
    You Can Get Help In:
    http://www.phpwind.net 
     
    碰到类似这样的问题是应为数据库表损坏了,简单的方法是在phpmyadmin中修复此表,不过这不一定能修复,如果不行就用命令去修复
    在命令行中找到mysql/bin/下的 mysqlcheck
    具体的语句为:
    mysqlcheck -r -uroot -ppassword phpwind pw_threads
    其中-r 代表修复, root代表数据库用户名, password代表数据库密码, phpwind为数据库名,pw_threads为所要修复的数据表,如果此处没内容,此操作对整个数据进行修复。
    当数据表较大时用此命令行修复,因为不会超时,如果用PHPMYADMIN来修复, 会超时导致修复不完全
  •  
    2008-03-17 17:14:59
    标签:smarty 杂谈

    test3.php:
    <?php
        require "main.php";
        $forum = array(
            array("category_id" => 1, "category_name" => "公告區",
                "topic" => array(
                    array("topic_id" => 1, "topic_name" => "站務公告")
                )
            ),
            array("category_id" => 2, "category_name" => "文學專區",
                "topic" => array(
                    array("topic_id" => 2, "topic_name" => "好書介紹"),
                    array("topic_id" => 3, "topic_name" => "奇文共賞")
                )
            ),
            array("category_id" => 3, "category_name" => "電腦專區",
                "topic" => array(
                    array("topic_id" => 4, "topic_name" => "硬體週邊"),
                    array("topic_id" => 5, "topic_name" => "軟體討論")
                )
            )
        );
        $tpl->assign("forum", $forum);
        $tpl->display("test

  •  
    2008-03-17 17:06:22
    标签:smarty 杂谈
    【摘 要】在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。

     〇. 引言
        在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您快速入门。

      一. 安装

        首先打开http://smarty.php.net/download.php,下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来我演示给大家一个安装实例,看过应该会举一反三的。

        (1) 我在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩隼吹哪柯嫉膌ibs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/,

        (2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" c>
    <title>Smarty</title>
    </head>
    <body>
    {$hello}
    </body>
    </html>

         新建index.php,将此文件放在learn/下:

    <?php
    //引用类文件
    require 'smarty/libs/Smarty.class.php';
    $smarty = new Smarty;
    //设置各个目录的路径,这里是安装的重点
    $smarty->template_dir = "smarty/templates/templates";
    $smarty->compile_dir = "smarty/templates/templates_c";
    $smarty->config_dir = "smarty/templates/config";
    $smarty->cache_dir = "smarty/templates/cache";

    //smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当
  •  
    2008-03-17 17:00:09
    标签:smarty 杂谈

    1 .include_once语句:

    引用文件路径,路径必需正确。

    2 $smarty=new Smarty();

    新建一个对象smarty, 实例化一个对象。

    3 $smarty->template_dir=“”

    指定$smarty对象使用的tpl模板的路径,它是一个目录,默认目录为当前的templates的目录,实际编程中,可能要指定目录。

    4 $smarty->compile_dir=””;

    指定$smarty对象的编译时的目录,就是smarty编译模板的目录,linux服务器,请你确认有可写可读权限。通常chmod -R 777 filename 修改权限,默认情况下它编译目录是当前的目录下的templates_c

    5 $smarty->left_delimiter$smarty->right_delimiter;

    查找模板变量左右的分割符,默认情况下为{ } 为了与script中括号相区别,通常写为<{ }>.

    6 $tp1->cache_dir=”./”;

    模板文件缓存的位置,

  •  
    2008-03-17 13:10:56
    标签:.htaccess 杂谈
     

    .htaccess可以做大量范围的事情,包括:文件夹密码保护、用户自动重新指向、自定义错误页面、变更你的文件扩展名、屏蔽特定的用户IP地址、只允许特定的IP地址、停止目录表以及使用其他文件作为index文件,等等......

    1. Introduction 介绍
    文件名 .htaccess 属性 644 (RW-R–R–)
    htaccess会影响它所在目录下的所有子目录
    注意大多数内容都要求保持在一行之内,不要换行,否则会引起错误

    2. Error Documents 错误文档
    Official document: ErrorDocument Directive
    ErrorDocument code document
    例子
    ErrorDocument 400 /errors/badrequest.html(www.iocblog.net)
    ErrorDocument 404 http://yoursite/errors/notfound.html
    ErrorDocument 401 “Authorization Required”
    (注意之后内容如果出现的双引号需要转义为 \”)
    常见HTTP状态码
    Successful Client Requests
    200 OK
    201 Created
    202 Accepted
    203 Non-Authorative Information
    204 No Content
    205 Reset Content
    206 Partial Content
    Client Request Redirected
    300 Multiple Choices
    301 Moved Permanently
    302 Moved Temporarily
    303 See Other
    304 Not Modified
    305 Use Proxy
    Client Request Errors
    400 Bad Request
    401 Authorization Required
    402 Payment Required (not used yet)
    403 Forbidden
    404 Not Found
    405 Method Not Allowed
    406 Not Acceptable (encoding)
    407 Proxy Authentication Required
    408 Request Timed Out
    409 Conflicting Request
    410 Gone
    411 Content Length Required
    412 Precondition Failed
    413 Request Entity Too Long
    414 Request URI Too Long
    415 Unsupported Media Type
    Server Errors
    500 Internal Server Error
    501 Not Implemented
    502 Bad Gateway
    503 Service Unavailable
    504 Gateway Timeout
    505 HTTP Version Not Supported

    3. Password Protection 密码保护
    Official document: Authentication, Authorization and Access Control
    假设密码文件为.htpasswd
    AuthUserFile /usr/local/safedir/.

  •  
    2008-02-25 18:23:15
    标签:python 杂谈
    1. 处理一个或多个文件的每一行:fileinput 模块
     
    import fileinput, sys, string
    # 从sys.argv 里取第一个参数并赋值给searchterm
    searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]
    for line in fileinput.input():
       num_matches = string.count(line, searchterm)
       if num_matches: # 大于零表示有匹配
       print "found '%s' %d times in %s on line %d."% (searchterm,   +\ num_matches,fileinput.filename(), fileinput.filelineno())
    如果这个脚本称为mygrep.py,它可以这样用:
    % python mygrep.py in *.py
    found 'in' 2 times in countlines.py on line 2.
    found 'in' 2 times in countlines.py on line 3.
    found 'in' 2 times in mygrep.py on line 1.
    found 'in' 4 times in mygrep.py on line 4.
    found 'in' 2 times in mygrep.py on line 5.
    found 'in' 2 times in mygrep.py on line 7.
    found 'in' 3 times in mygrep.py on line 8.
    found 'in' 3 times in mygrep.py on line 12.
    2.
      停止执行程序:sys.exit()
      时间格式:time.strftime('%Y%m%d')
      文件或目录是否存在:os.path.exists(target_dir)
      创建目录: os.mkdir(target_dir)
      当前目录:os.curdir
      指定目录的文件名列表:os.listdir(目录名)
      重命名:os.rename
      例子:
      import os, string
    if len(sys.argv) == 1: # 如果没有指定目录
        fil
  •  
    2008-02-20 13:58:55
    标签:python 杂谈
     

    6. 异常和文件处理

    6.1 异常处理.

    Python的异常
    try
    except
    finally
    来处理. 并且except后还可以跟 else .
    引发异常用
    raise

    如果抛出的异常没有被处理. 在Python IDE中是显示一些红色的信息. 在真正的Python程序运行时. 会导致程序终止.

    在以前我们已经见到过一下几种异常:

    在 Dictionary 中如果使用的 key 不存在. 会引发 KeyError 异常. 如:
    >>> d = {"a":1, "b":"abc"}
    >>> d["c"]
    Traceback (most recent call last):  File "<interactive input>", line 1, in <module>
    KeyError: 'c'

    搜索列表中不存在的值. 将引发 ValueError 异常. 如:
    >>> li = [1,2]
    >>> li.index(3)
    Traceback (most recent call last):  File "<interactive input>", line 1, in <module>
    ValueError: list.index(x): x not in list

    对应的. 若用下标来引用列表中的元素. 若下标出界. 会产生 IndexError 异常. 如:
    >>> li[2]
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    IndexError: list index out of range

    调用不存在的方法. 会引发 AttributeError 异常.
    引用不存在的变量. 引发