Archive for the 'PHP' Category


Category: PHP     Comments    

在Zend Framework 中使用 PDO_ODBC

其实只需要小改一下Zend_Db_Adapter_Pdo_Mssql就可以支持ODBC

function _dsn()

判断PDO Type 的时候加多一个

case 'odbc':
$this->_pdoType = 'odbc';
break;

再增加一個方法

public function quote($value)
{
if (is_int($value) || is_float($value)) {
return $value;
}
return "'".addslashes($value)."'";
}

然后,传递参数的时候参考下面的例子:

// 数据库连接参数
$params = array(
'SERVER' => "192.168.1.2"
'DATABASE' => "king",
'username' => "king",
'password' => "kingpsw",
'dbname' => "king",
'pdoType' => "odbc",
'DRIVER' => "SQL Server",
);
// 单件模式
if (!Zend_Registry::isRegistered("db")) {
$db = Zend_Db::factory('Pdo_Mssql', $params);
Zend_Registry::set("db", $db);
}else {
$db = Zend_Registry::get("db");
}

使用上面的参数会生成类似
odbc:DRIVER=SQL Server;SERVER=192.168.1.2;DATABASE=king;
这样的Data Source Name (DSN) 串,这样就会通过ODBC连接数据库了,当然要记得打开 php_pdo_odbc.dll 扩展

虽然使用ODBC驱动可以避免SQL SERVER 中VARCHAR长度超过255的限制,但是也并不是这样就可以解决问题,PDO_ODBC存在一个BUG:
http://bugs.php.net/bug.php?id=40452 上面有些评论可以参考一下
当INSERT的时候DATETIME字段会出现以下错误:

[Microsoft][ODBC 驅動程式管理員] 函數順序錯誤
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY010]: Function sequence error: 0 [Microsoft][ODBC Driver
Manager] Function sequence error (SQLExecute[0] at
ext\pdo_odbc\odbc_stmt.c:133)'

參考:http://framework.zend.com/issues/browse/ZF-905

Category: PHP     Comments    

关于Zend_Db_Adapter_Pdo_Mssql最后一分页问题的解决方法

Zend framework (我当前使用的版本是1.5) 中的MSSQL(SQL SERVER)PDO Adapter(Zend_Db_Adapter_Pdo_Mssql)分页是有问题的,多数情况最后一页总会和n-1页的记录重叠,经测试Zend Framework 1.7.0里面的Zend_Db_Adapter_Pdo_Mssql同样存在此问题

当有LIMIT的时候,分析ZF_DB生成的SQL语句:

SELECT * FROM
(
SELECT TOP 30 * FROM (
SELECT TOP 60 "package_id", "title_eng"
FROM "hotel_package" ORDER BY "package_id" DESC
) AS inner_tbl ORDER BY "package_id" ASC
)
AS outer_tbl ORDER BY "package_id" desc

上面的SQL是PAGE=2,是每页30笔记录,30就相当于MYSQL中的LIMIT,60就是OFFSET

看不懂上面的SQL的话可参考这里:
http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html

Zend_Db_Select中limitPage方法中有如下代码:

$this->_parts[self::LIMIT_COUNT]  = (int) $rowCount;
$this->_parts[self::LIMIT_OFFSET] = (int) $rowCount * ($page - 1);

显然,这个SQL的结果是每一页都是30笔,最后一页也是。
当 “hotel_package” 中有31笔记录的时候,一共有2页,翻到第2页的时候也显示30条记录是不对的
当然这不太影响使用,但是挑剔的人会提出质疑,这也无可厚非,但是怎么ZEND也不重视这个问题
或者说ZEND并不重视ZF中的MSSQL

由于ZF本身的问题,所以不得不修改ZF的代码,这种情况是在万不得已经的情况下才去做…

首先考虑是修改 Zend_Db_Adapter_Pdo_Mssql 这个类,因为是Adapter所以可以自己另存一个,改一下类和文件名,是最好的方法并不影响到ZF以后的升级更新,不过另存修改Zend_Db_Adapter_Pdo_Mssql并不现实,因为传给limit的参数有限:
limit($sql, $count, $offset = 0)
没法判断是否最后一页是否小于limit,没办法,只能把目光转移到Zend_Db_Select中的limitPage
修改如下:

public function limitPage($page, $rowCount, $tail = NULL)
if ($tail > 0 && !is_bool($tail)) {
$this->_parts[self::LIMIT_COUNT]  = (int) $tail;
}else {
$this->_parts[self::LIMIT_COUNT]  = (int) $rowCount;
}

然后在自己的分页类中,添加一个tail的返回值

// pagerInfo 保存关于分页信息的数组
$pagerInfo['_tail'] = NULL;
// 判断是否最后一页
if ($page == $pagerInfo['pagesCount']) {
// 获取余数
$tail = $rows_count % $rowCount;
if ($tail != 0)   $pagerInfo['_tail'] = $tail;
}

在目前最新的Zend Framework 1.7.0 中增加这样这两个特性
Zend_Db_Table_Select adapater for Zend_Paginator
Support for custom adapters in Zend_Paginator
看来是一个分页功能,不知道是怎样处理上面的问题的,下次再去了解一下

Category: PHP     Comments    

关于PHP只能读取MsSQL的char/varchar字段前256个字符的问题

很不幸地用PHP操作MsSQL(SQL Server)数据库的时候,如果您的char或者varchar字段长度超过256的话只能读取到前256个字符的内容,后面的会被无情的CUT掉,无论你用最基本的MSSQL DB库还是PDO_MSSQL,或者自己写一个专门的MSSQL DB扩展也无济于事,因为PHP操作MSSQL都是通过微软提供的SQL Server Client Library工作,也就是ntwdblib.dll 这个文件,目前“最新”版本是2000.80.194.0(需要自行去搜索下载),就算最新版本(PHP 5.3 alpha2)的PHP自带的都是这个98年的版本2000.2.8.0,“最新” 版本估计是2000年的产物(根据这个判断© 1988-2000 Microsoft Corp. 保留所有权利。)难怪2001年提交的这个BUG至今还没有被FIX

http://bugs.php.net/bug.php?id=11593

调整php.ini中的 mssql.textlimit 和 mssql.textsize 也没用,这两个参数形同虚设…

鉴于这个库的版本太老,用PHP+MsSQL这种鸡肋组合的同志们要小心了…

解决办法就是使用ODBC驱动,请参考:
在Zend Framework 中使用 PDO_ODBC

Category: PHP     Comments    

FASTCGI PHP ON IIS6 非正式测试

测试环境是建立在虚拟PC上的,执行ApacheBench的是宿主,虚拟PC软件是用微软的VPC,上面装了WIN2003简体,RAM是共享宿主的512MB

宿主的配置是:T2400 1.8G 2.5G RAM

———————————————————–

本次测试是针对在没有微软官方的FASTCGI之前在IIS上使用PHP(不论是ISAPI方式或者CGI方式),在高负载和长期运行后不稳定的问题进行的,本次测试目的主要是检查FASTCGI PHP的稳定性

———————————————————–
被测试的PAGE只是一个phpinfo()调用.

使用的是ApacheBench模拟30个并发,执行1000000次请求,结果请看下面.
在三个半小时(214分钟)的整个测试过程中没有出现任何异常,测试完毕后RAM也被正常释放.

fastcgi testing

fastcgi testing

PS.在测试期间宿主上面还打开多个聊天软件并且打开多个IE在进行浏览网页

hp-cgi.exe进程

FASTCGI的配置如下

PHP=C:\PHP\php-cgi.exe
[C:\PHP\php-cgi.exe]
QueueLength=1000
MaxInstances=4
InstanceTimeout=30
InstanceMaxRequests=1000
------------------------ApacheBench結果---------------------
ab -c 30 -n 1000000 http://www.vpc.com/phpinfo.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/
Benchmarking www.vpc.com (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Finished 1000000 requests
Server Software: Microsoft-IIS/6.0
Server Hostname: www.vpc.com
Server Port: 80
Document Path: /phpinfo.php
Document Length: 50211 bytes
Concurrency Level: 30
Time taken for tests: 12844.317000 seconds
Complete requests: 1000000
Failed requests: 138046
  (Connect: 0, Length: 138046, Exceptions: 0)
Write errors: 0
Total transferred: -1130766272 bytes
HTML transferred: -1328766272 bytes
Requests per second: 77.86 [#/sec] (mean)
Time per request: 385.330 [ms] (mean)
Time per request: 12.844 [ms] (mean, across all concurrent requests)
Transfer rate: -85.97 [Kbytes/sec] received
Connection Times (ms)
  min mean[+/-sd] median max
Connect: 1 3 5.1 2 2336
Processing: 42 381 180.5 348 6493
Waiting: 30 364 179.2 331 6469
Total: 45 385 180.8 351 6495
Percentage of the requests served within a certain time (ms)
  50% 351
  66% 380
  75% 402
  80% 420
  90% 484
  95% 562
  98% 753
  99% 1168
 100% 6495 (longest request)

Category: PHP     Comments    

在IIS6下配置官方的FASTCGI使用PHP

1.首先到微軟官方IIS網站下載,並安裝 FastCGI Extension for IIS6.0 - RTM
http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1521
(安裝後不需要重啟SERVER)

2.設置IIS
設置支持.php文件

  1. Double click the machine icon for the local computer
  2. Right click on “Web Sites” and pick “Properties”
  3. Click on the tab labeled “Home Directory”
  4. Click on the “Configuration…” button.
  5. Click on the “Add…” button.
  6. Browse to %WINDIR%\system32\inetsrv\ to select fcgiext.dll as the Executable.
  7. Enter .php as the Extension
  8. Select “All verbs”
  9. Ensure that “Script Engine” and “Verify that file exists” are checked.
設置.php擴展名映射

添加影射

設置默認首頁

  1. Double click the machine icon for the local computer.
  2. Right click “Web Sites” and pick “Properties.”
  3. Click the “Documents” tab.
  4. Click the “Add…” button and enter “index.php” for “Default Document Name:”
  5. Click “OK.”
IIS中設置缺省index.php首頁

IIS中設置缺省index.php首頁

3.配置PHP FastCGI

編輯%WINDIR%\system32\inetsrv\fcgiext.ini
在[Types]下面加入
PHP=C:\PHP\php-cgi.exe
[C:\PHP\php-cgi.exe]
QueueLength=1000
MaxInstances=4
InstanceTimeout=30
InstanceMaxRequests=200

上面的參數根據實際環境進行配置

configuration settings (per process pool):
fullPath: path to application for this pool (e.g. d:\php\php-cgi.exe, NOTE: must match handlers section)
maxInstances: maximum number of processes to create in this pool
instanceTimeout: maximum duration a request can last
instanceMaxRequests: process will be retired after exceeding this number of requests
protocol: use named pipes or tcp. The tech preview currently only supports named pipes, but TCP should be simple to add.
queueLength: maximum number of requests to queue (when this is exceeded the module will start 500'ing)
flushNamedPipe: Flush the named pipe at the end of each request. This is to workaround, what seems to be a bug in libfcgi: if two FastCGI requests arrive in a single ReadFile call, libfcgi closes the pipe. (If anyone wants to tackle this problem, I'd be happy to provide a repro)

4.配置php.ini
設置好extension_dir並打開需要的extension

; fastcgi.impersonate = 1; 改成 fastcgi.impersonate = 1
; cgi.fix_pathinfo=1 改成 cgi.fix_pathinfo=1
; cgi.force_redirect = 1 改成 cgi.force_redirect = 0

更詳細的安裝說明可以參考
%WINDIR%\system32\inetsrv\fcgireadme.htm
http://learn.iis.net/page.aspx/247/using-fastcgi-to-host-php-applications-on-iis-60/

PS
當你已經打開過php文件後,你再去修改php.ini必須用命令行重啟IIS才會生效

net stop w3svc
net start w3svc

Next Page »

Copyright © 2005-2009 kingchan.net, All rights reserved, Wordpress-Theme by Felix Krusch 粤ICP备09019789号