php handlersocket

一、安装php-handlersocket模块:

php-handlersocket, PHP extension for interfacing with MySQL Handler Socket.

wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.1.0.tar.gz
tar zxvf php-handlersocket-0.1.0.tar.gz
cd php-handlersocket
phpize
./configure
make
make install

二、php-handlersocket 使用示例:

	/*
	 * 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);
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', 'aaa@dsf.com', '2011-04-07 18:26:03');
INSERT INTO `user` VALUES ('2', 'bbb', 'bbb@dsf.com', '2011-04-07 18:26:03');
INSERT INTO `user` VALUES ('3', 'ccc', 'ccc@test.com', '2011-04-07 18:26:03');
< ?php
$host = '192.168.5.28';
$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', 'xun@dsf.com', '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', 'xun@dsf.com', '2011-04-07 18:26:03')) === false)
{
    echo $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array('6', 'aaa6', 'xun@dsf.com', '2011-04-07 18:26:03')) === false)
{
    echo 'A', $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array('7', 'aaa7', 'xun@dsf.com', '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();
}
?>

Related posts:

  1. 一、 php与XML、XSLT、Mysql的结合运用,安 一 php与XML、XSLT、Mysql的结合运用,安装篇———————————————————————- 原创: 作者:xiaocon 邮箱:xiaocon@21cn.com 一 php与XML、XSLT、Mysql的结合运用,安装篇经常看到有用户问一些关于php与XML、数据库结合运用的贴子,也经常看到一些初学者把php代码与HTML代码混写到一起,然后在出错的时候找不到错误,急得团团转,下面我就给大家讲一下如何用XML技术将HTML代码和PHP程序分离,当然,分离的技术有好多种,比如PEAR中的IT模板.总的来说,个人认为使用XML技术是最方便的,废话不多说了,我们言归正传,讲一下在win2000下的安装使用php中的xml与xslt要用到一些dll库,extension=php_domxml.dll //操作XML的函数库extension=php_iconv.dll //转码用的,比如将GB2312的转成UTF-8的extension=php_xslt.dll //XSLT的函数库...
  2. strtotime()函数 int strtotime ( string time [, int now]) 本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX...
  3. MySql常用语法整理-表操作 检查表CHECK TABLE table_name;修复表REPAIR TABLE table_name;优化表OPTIMIZE TABLE table_name;分析表ANALYZE TABLE table_name;清空表TRUNCATE table_name;...
  4. MySQL插件HandlerSocket HandlerSocket 是MySQL的一个插件,用来实现 NoSQL 功能,用于跳过MySQL的SQL层面,直接访问内部的InnoDB存储引擎。 wget http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-client-5.5.11-1.rhel4.i386.rpm wget http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-devel-5.5.11-1.rhel4.i386.rpm wget http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-server-5.5.11-1.rhel4.i386.rpm...
  5. 在CentOS及Ubutun下安装Percona-Server和HandlerSocket注意事项 见: http://www.percona.com/docs/wiki/repositories:start 一、CentOS下安装Percona-Server及HandlerSocket 安装Percona Yum Repository rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.i386.rpm rpm...

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>