Archive for the ‘Programming/php’ Category.
wordpress 的 schema
ver 2.7:
幾個查詢例:
SELECT m.* FROM wp_posts p , wp_postmeta m where p.id=m.post_id order by post_id desc SELECT tr.* FROM wp_posts p , wp_term_relationships tr where p.id=tr.object_id order by p.id desc SELECT p.id , w.* FROM wp_posts p , wp_term_relationships tr , wp_term_taxonomy t where p.id=tr.object_id and tr.term_taxonomy_id=t.term_taxonomy_id order by p.id desc SELECT p.id , w.* FROM wp_posts p , wp_term_relationships tr , wp_term_taxonomy t , wp_terms w where p.id=tr.object_id and tr.term_taxonomy_id=t.term_taxonomy_id and t.term_id = w.term_id order by p.id desc
gentoo emerge php options
2009.0630 Monster LAMP Pack:
取消 :
ncurses pdo readline spell
增加 :
ctype json sockets
berkdb gdbm
xmlreader xmlwriter
oci8-instant-client
USE="apache2 berkdb bzip2 calendar cjk cli crypt ctype curl gd gdbm hash iconv json mysql mysqli nls oci8-instant-client pcre reflection session simplexml sockets spl ssl tokenizer truetype unicode xml xmlreader xmlwriter zlib" emerge -av php
2009.0630 發現 wordpress code 裡面有用到 token_get_all ( tokenizer 所以不能取消)
sqlrelay sample code / prepare statement / bind value 寫法
include dirname(__FILE__) . '/local_config.php';
$__oradb=_fn_connect_sqlrelay();
$__oradb->setOption('portability', DB_PORTABILITY_LOWERCASE);
$sql = '
select g_no,ctrl_rowid,g_storage,g_img
from goods_file
where g_no=?
';
foreach ( $items as $g_no => $v ) {
$prepare = $__oradb->prepare($sql);
$result = $__oradb->execute($prepare,$g_no);
if ( $result ) {
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
$items[ $g_no ]['ctrl_rowid'] = $row['ctrl_rowid'];
$items[ $g_no ]['g_storage'] = $row['g_storage'];
$items[ $g_no ]['g_img'] = $row['g_img'];
$result->free();
} else continue;
}
$__oradb->disconnect();
Monster LAMP Pack Lite – ver.317
Basic install emerge
lilo dhcpcd openssh syslog-ng vixie-cron screen ntp cronolog net-mail/mpack app-arch/sharutils unzip bind-tools trafshow traceroute
Linux : 2.6.24-gentoo-r7
Apache : 2.2.10
Mysql : 5.0.70-r1
PHP : 5.2.8-pl2
PHP 的編法是
USE="apache2 berkdb bzip2 calendar cjk cli crypt curl gd gdbm hash iconv json mysql mysqli ncurses nls oci8-instant-client pcre readline reflection session simplexml spell spl ssl truetype unicode xml zlib" emerge -av php
package 有
samba :
postfix :
oracle instant client :
open-vm-tools : vmware 的 tools
emerge 這些 package:
重要軟體:
mysql php apache postfix
中等重要:
screen ntp samba
工具類軟體:
subversion vim open-vm-tools cronolog net-mail/mpack app-arch/sharutils unzip
記得
- /etc/udev/rules.d/70-persistent-net.rule 砍掉
- 改 net_DHCP
- 砍 /tmp/*
- check /etc/conf.d/clock , /etc/hosts , /etc/resolv.conf
Hypertext Transfer Protocol — HTTP/1.1 – 這可是現今最偉大的 protocol 呀!
The Hypertext Transfer Protocol (HTTP) is an application-level
protocol for distributed, collaborative, hypermedia information
systems. It is a generic, stateless, protocol which can be used for
many tasks beyond its use for hypertext, such as name servers and
distributed object management systems, through extension of its
request methods, error codes and headers
http://tools.ietf.org/html/rfc2616
Status Code Definitions
http://tools.ietf.org/html/rfc2616#section-10
204 No Content
The server has fulfilled the request but does not need to return an
entity-body, and might want to return updated metainformation. The
response MAY include new or updated metainformation in the form of
entity-headers, which if present SHOULD be associated with the
requested variant.
If the client is a user agent, it SHOULD NOT change its document view
from that which caused the request to be sent. This response is
primarily intended to allow input for actions to take place without
causing a change to the user agent’s active document view, although
any new or updated metainformation SHOULD be applied to the document
currently in the user agent’s active view.
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
可以利用這點作一些事, 譬如記錄 user 的訪問, log 等等
PHP Coding Standards
參考: Mantis : http://www.mantisbt.org/guidelines.php
中文翻譯: http://www.ichiayi.com/wikipage/tech/mantis_coding
First, start off by reading the PHP Coding Standards document. I’ve deviated in a few places but just about everything in the document applies here as well.
Above all, write code that is easy to read and maintain. Comment blocks of code and functions at all times. And get on my case if I deviate too much as well!
Naming Variables
- Use all lower case.
- Use _ to separate words, e.g. $green_color_value
- Use descriptive names (except loop variables).
- Loop variables can be of the usual variety: $i, $j, $k, etc.
- Count variables should follow the format $*_count, e.g. $bug_count
- Global variables should be prefixed with g_
- Temporary variables should be prefixed with t_
- Parameters and variables passed from forms that have been cleaned of any special SQL chars (i.e. slashes) should be prefixed with c_
- Uncleaned function parameters should be prefixed with p_
- Uncleaned variables passed from forms should be prefixed with f_
- Other variables are prefixed with v_, v2_, etc.
- Never prefix with l_ or o_ or q_ (visually confusing)
- $query and $result should be used for SQL query and results respectively
Naming Functions
- Use all lower case.
- Use _ to separate words, e.g. setup_page_breaks()
- Keep functions to 5 words or less
- Functions that print should be prefixed with print_.
- Try to use prefixes to group functions (i.e., email_, news_, etc.)
Naming Classes
- Use FirstLetterOfWordIsCaptilized style
- Variables that are class objects should have the prefix coo_
Naming Files
- Use all lower case.
- Use _ to separate words, e.g. view_new_bugs_page.php
- Use .php file extensions
- Filenames must be less than 32 characters in length. This plays nice with older file systems like Mac OS.
- Included files should be suffixed by _inc.php
SQL formatting
- UPPERCASE all SQL keywords:
$query = "SELECT * FROM $g_mantis_bug_table WHERE id='1'";
- Always assign a query string to a variable. This makes code easier to debug when problems occur. Do not create the query in the call to the function.
- Break up SQL queries over multiple lines to be easy to read.
General Formatting
- Use TABS with a size of 4
- Follow the table formatting of existing pages
- Use <?php ?> for php delimiters.
- Try not to print/echo HTML unless it’s short or in a function loop
- Do not use the EOF construct
Miscellaneous
- Don’t use the ?: construct except in very rare cases. It is confusing and has a lot of bug potential.
- Avoid magic numbers. The only magic numbers in use should be 1 and 0 and their meaning should be obvious.
Page Guidelines
- The first item should be the copyright notice
- At the bottom will be the footer information and closing print_ functions.
Braces and Parantheses
- Parantheses should be right after a function name, e.g. function() not function ()
- Parantheses should have a space right after a keyword (if, while, for), e.g. for (…)
- Formatting of braces is illustrating below. We use unmatched placing.
- Arrays should be referenced with no spaces, e.g. $arr['index'] not $arr[ 'index' ]
for (...) { blah } or if (...) { blah } - if … else blocks should be in this format:
if (...) { blah1 } else { blah2 }
Comparisons
- The NOT operator should be placed next to its operand. No spaces, e.g. !$value
- Parentheses should be used for grouping, especially with multiple comparisons, e.g. if ( ( null == $val ) && ( null == $val2 ) )
Strings
- Use spaces around the string concatenation operator, e.g. ‘str ‘ . $value . ‘ str2′;
- Use ‘ instead of " if there are no variables or special characters.
Comments
- Use the # symbol for line commenting, not //
- Use /* */ for block commenting unless you nest /* */ comments. Generally, only use this during development.
- Use @@@ followed by a brief message (BROKEN, TEMPORARY, etc) as a "look at this" indicator. Leaving your name next to is a good idea. This way critical items can easily be found.
Editor Features
- Search and replace in multiple files
- Goto line number
- Syntax highlighting
- Adjustable TAB spacing
用 pecl inclued 產生關聯圖
這是一隻用 codeigniter 寫的一個簡單的程式 , 然後用 pecl inclued 把它的 include/require 關聯圖畫出來
參考 URL : http://baniclin.blogspot.com/2008/09/pecl-inclued.html
oci / compile php with oracle instant client
小筆記一下…
oracle 那邊 download 這幾個 file
-rw-rw-r– 1 root portage 44414338 Feb 24 13:41 instantclient-basic-linux32-11.1.0.7.zip
-rw-rw-r– 1 root portage 607196 Feb 24 13:41 instantclient-sdk-linux32-11.1.0.7.zip
放到 /usr/portage/distfiles 這底下
然後 PHP 是這麼編的
USE="apache2 berkdb bzip2 calendar cjk cli crypt curl gd gdbm hash iconv json mysql mysqli ncurses nls oci8-instant-client pcre readline reflection session simplexml spell spl ssl truetype unicode xml zlib" emerge -av php
中文 字詞 文章 索引 對照規畫
tbl_term
| term_id | term |
| 1 | ipod |
| 2 | apple |
| 3 | nano |
| 4 | iphone |
tbl_doc
| doc_id | text_content | uptime |
| 1 | 【蘋果先生】Moshi iGlaze Apple iPhone 3G 專用保護殼 黑/紅/白 | … |
| 2 | iTunes Gift Card蘋果線上音樂商店預付卡儲值Apple iTunes Shop Ipod Nano Shuffle iphone Mp3電影歌曲下載促銷 | … |
| 3 | Apple iPhone 3G (8G) |
tbl_lookup
| id | term_id | doc_id |
| 1 | 2 | 1 |
| 2 | 4 | 1 |
| 3 | 2 | 2 |
| 4 | 1 | 2 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
| 7 | 2 | 3 |
| 8 | 4 | 3 |
another high performance PHP framework – Yii
他有作 performance compare – 據說是最快的 PHP framework
看他的 features 寫的 , 一些 “先進” 的 framework 的功能他都具備
- DAO , active record
- Web 2.0 widget , 這個酷
- Theming , layered caching scheme 這個 ci 沒有
- 2A , authentication and authorization , role-based 這個拿來作後台管理功能應該很方便
不過 那條紅色的線 可以不用看 , 看藍色那條是比 codeigniter 快 1/3 , 有空要來玩一下
http://www.yiiframework.com/performance

簡單方便的 RSS 工具程式 Feedcreator
include("include/feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/".$PHP_SELF;
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
}
$rss->saveFeed("RSS1.0", "news/feed.xml");
我的 subversion / svn server 設定
OS : Gentoo Linux 2.6.22-gentoo-r5 #11
gcc –version
gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.1)
svnserve –version
svnserve, version 1.5.4 (r33841)
compiled Dec 17 2008, 08:11:10
/usr/bin/svnserve –foreground –daemon –root=/var/svn
很奇怪 , 裝法都一樣, 但我另一台 MLP Lite 就不 work??

一些 subversion command 下的操作:
用 svn co 一個 source 到目錄下 , 把要加到 svn 的檔案/目錄 copy 過來 , 下 svn status 會看到一堆 ? 表示那些是新的檔案
下 svn add * 或 svn add dirname
下 svn status 可以看到 剛剛的 ? 變成 A 了 (add)
下 svn ci 把 source code commit 進 svn
cd 到工作目錄
下
svn –username monster export svn://xxx.com/my_repos/search/src
把 source code export 出來
簡單用一個方法來判斷目前是啥環境
阿毛找到這個方式 , 我覺得很可行 ….
在 php.ini ( apache 跟 cli 兩個目錄下都要設定) 的最後面加上
my_environment = product
( or dev / stage / stage2 / product )
我們在程式中用這方法來判斷, 目前是啥環境…
if ( get_cfg_var(‘my_environment’)==’dev’ ) {
// setting for dev
} else if ( get_cfg_var(‘my_environment’)==’stage’ ) {
// setting for stage
} else if ( get_cfg_var(‘my_environment’)==’stage2′ ) {
// setting for stage2
}
// default setting is production
解決 memcache race condition 的問題
解決辦法就是避免所有的 query 集中在那個 expire 時間點上 , 那該 user 的方法是每次的 get 時加上一個亂數的時間差 .
http://www.alexhopmann.com/2008/06/13/memcache-race-condition-fun/
memcache 的作者認為, user “不應該” 關心他的資料為什麼不再 memcache 裡 , 就算是 memcache server 都關掉, 都不能影響程式的進行, 頂多程式變慢而已.
[code language='php']
$key='key';
$obj = $memcache->get($key);
if ( $obj==NULL ) {
$obj = Get_obj_from_table();
$memcache->set($key, $obj, $expire);
}
[/code]
測試心得是:
- 如果 memcached server 的 memory 滿了, 他的 bytes / curr_items 就不會再增加了 , 但是 client 還是可以 set key / get key 沒問題
- 如果關掉某些 memcache server , 所有的 set 動作 “會” 再重新分配到其他活的 server 上, 要注意 memcache server 群的容量問題, 在 expire 時間內還是可以抓到資料, 不過還是有隨機的 set 是 fail 的(不多啦!)
- 如果 add的一堆 server 都掛了 , 那麼會 hang 在 set/get 上面 , 等 tcp timeout
USE Oracle / oci8 with PHP
前提環境要先裝好 Oracle instant client , 然後 PHP 要把 instantclient 編譯進去
–with-oci8=instantclient,/usr/lib/oracle/10.2.0.3/client/lib
這是 sample code
$conn = OCILogon('scott', 'scott', '//172.30.0.176/DB1');
$query = 'select * from dept';
$stid = OCIParse($conn, $query);
OCIExecute($stid, OCI_DEFAULT);
while ($succ = OCIFetchInto($stid, $row)) {
foreach ($row as $item) {
echo $item." ";
}
echo "\n";
}
OCILogoff($conn);
心得是
1. 建 oracle table 時不管key 的 table name , field name 是否為小寫, 在 oracle 都顯示成大寫
2. 用 PHP 的 OCI functions 下的 SQL command 也可以不管大小寫
3.

