php-handlersocket, PHP extension for interfacing with MySQL Handler Socket.
从 http://php-handlersocket.googlecode.com/获得php handlersocket
tar zxvf php-handlersocket-0.3.0.tar.gz cd php-handlersocket phpize ./configure make make install
二、php-handlersocket 使用示例:
先准备一段SQL后面使用
CREATE TABLE `user` ( `user_id` int(10) unsigned NOT NULL, `user_name` varchar(50) DEFAULT NULL, `user_email` varchar(255) DEFAULT NULL, `created` datetime DEFAULT NULL, PRIMARY KEY (`user_id`), KEY `INDEX_01` (`user_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'aaa', '[email protected]', '2011-04-07 18:26:03'); INSERT INTO `user` VALUES ('2', 'bbb', '[email protected]', '2011-04-07 18:26:03'); INSERT INTO `user` VALUES ('3', 'ccc', '[email protected]', '2011-04-07 18:26:03');
API例子说明
/* * String $host:MySQL ip; * String $port:handlersocket插件的监听端口,它有两个端口可选:一个用于读、一个用于写 */ $hs = new HandlerSocket($host, $port); 打开一个数据表: /* * Int $index:这个数字相当于文件操作里的句柄,HandlerSocket的所有其他方法都会依据这个数字来操作由这个 openIndex打开的表, * String $dbname:库名 * String $table:表名 * String $key:表的“主键”(HandlerSocket::PRIMARY)或“索引名”作为搜索关键字段,这就是说表必须有主键或索引 * 个人理解:要被当做where条件的key字段,这样可以认为handlersocket只有一个where条件 * String $column:'column1,column2' 所打开表的字段(以逗号隔开),就是说$table表的其他字段不会被操作 */ $hs->openIndex($index, $dbname, $table, $key, $column); 查询: /* * Int $index: openIndex()所用的$index * String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解为where条件 * Array $value * Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数 * Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数 */ $retval = $hs->executeSingle($index, $operation, $value, $number, $skip); 插入(注意:此处的openIndex要用$port_wr,即读写端口): /* * Int $index: openIndex()所用的$index * Array $arr:数字元素数与openIndex的$column相同 */ $retval = $hs->executeInsert($index, $arr); 删除(注意:此处的openIndex要用$port_wr,即读写端口): /* * Int $index: openIndex()所用的$index * String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解为where条件 * Array $value * Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数 * Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数 */ $retval = $hs->executeDelete($index, $operation, $value, $number, $skip); 更新(注意:此处的openIndex要用$port_wr,即读写端口): /* * Int $index: openIndex()所用的$index * String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解为where条件 * Array $value * Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数 * Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数 */ $retval = $hs->executeUpdate($index, $operation, $value, $number, $skip);
可运行的例子:
<?php $host = '192.168.1.101'; $port = 9998; $port_wr = 9999; $dbname = 'test'; $table = 'user'; //GET $hs = new HandlerSocket($host, $port); if (!($hs->openIndex(1, $dbname, $table, HandlerSocket::PRIMARY, 'user_id,user_name,user_email,created'))) { echo $hs->getError(), PHP_EOL; die(); } $retval = $hs->executeSingle(1, '>=', array('0'), 10, 0); var_dump($retval); $retval = $hs->executeMulti( array(array(1, '=', array('1'), 1, 0), array(1, '=', array('2'), 1, 0))); var_dump($retval); unset($hs); //UPDATE $hs = new HandlerSocket($host, $port_wr); if (!($hs->openIndex(2, $dbname, $table, '', 'user_name,user_email,created'))) { echo $hs->getError(), PHP_EOL; die(); } if ($hs->executeUpdate(2, '=', array('2'), array('aaa', '[email protected]', '2011-04-07 18:26:03'), 1, 0) === false) { echo $hs->getError(), PHP_EOL; die(); } unset($hs); //INSERT $hs = new HandlerSocket($host, $port_wr); if (!($hs->openIndex(3, $dbname, $table, '', 'user_id,user_name,user_email,created'))) { echo $hs->getError(), PHP_EOL; die(); } if ($hs->executeInsert(3, array('5', 'aaa5', '[email protected]', '2011-04-07 18:26:03')) === false) { echo $hs->getError(), PHP_EOL; } if ($hs->executeInsert(3, array('6', 'aaa6', '[email protected]', '2011-04-07 18:26:03')) === false) { echo 'A', $hs->getError(), PHP_EOL; } if ($hs->executeInsert(3, array('7', 'aaa7', '[email protected]', '2011-04-07 18:26:03')) === false) { echo 'B', $hs->getError(), PHP_EOL; } unset($hs); //DELETE $hs = new HandlerSocket($host, $port_wr); if (!($hs->openIndex(4, $dbname, $table, '', ''))) { echo $hs->getError(), PHP_EOL; die(); } if ($hs->executeDelete(4, '=', array('1')) === false) { echo $hs->getError(), PHP_EOL; die(); } ?>