设为首页 收藏本站
查看: 445|回复: 0

[经验分享] php handlersocket

[复制链接]

尚未签到

发表于 2017-3-3 10:00:40 | 显示全部楼层 |阅读模式
一、安装php-handlersocket模块:
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', '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');


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', '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();
}
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-349560-1-1.html 上篇帖子: php ajax 下篇帖子: debug PHP
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表