/** * 增加一条数据库记录 * * @param data 需要insert 的数据 */ public function insert($data) { $this->clearERR(); if ($this->server == false && $this->init() == false) { return false; } if ($this->check($data) == false) { return false; } $key = $this->config['KEY']; $fields = $this->config['FIELDS']; $keyConfig = $fields[$key]; if (!isset($data[$key]) && $keyConfig['auto'] == false) { $this->errCode = 20041; $this->errMsg = "dtc config:{$this->dtcKey} insert key not set"; return false; } $req = new tphp_ttc_request($this->server, TPHP_DTC_OP_INSERT); if (isset($data[$key])) { if ($this->key_type == 1) { $errCode = $req->set_key($data[$key]); } elseif ($this->key_type == 2) { $errCode = $req->set_key_str($data[$key]); } else { $this->errCode = 200106; $this->errMsg = 'request failed'; return false; } if ($errCode != 0) { $this->errCode = $errCode; $this->errMsg = "dtc config:{$this->dtcKey} insert:{$key}: set_key error"; return false; } } foreach ($data as $k => $v) { if ($k == $key) { continue; } if (!isset($fields[$k])) { continue; } $tmpConfig = $fields[$k]; switch ($tmpConfig['type']) { case DTC::INT: $data[$k] = $data[$k] + 0; // 确保类型正确,否则dtc扩展报warnning $req->set($k, $data[$k]); break; case DTC::STRING: $data[$k] = $data[$k] . ''; // 确保类型正确,否则dtc扩展报warnning $req->set_str($k, $data[$k]); break; case DTC::BINARY: $data[$k] = $data[$k] . ''; // 确保类型正确,否则dtc扩展报warnning $req->set_bin($k, $data[$k]); break; case DTC::FLOAT: $data[$k] = $data[$k] . ''; // 确保类型正确,否则dtc扩展报warnning $req->set_flo($k, $data[$k]); break; default: $this->errCode = 20043; $this->errMsg = "dtc config:{$this->dtcKey} field:{$k} data type:{$v['type']} error"; return false; } } $result = new tphp_ttc_result(); $req->execute($result); $this->result = $result; //TODO... $errCode = $result->result_code(); if ($errCode != 0) { $this->dtcCode = $errCode; $this->errCode = $errCode; $k_val = isset($data[$key]) ? $data[$key] : '[auto_increment]'; //$this->errMsg = "dtc config:{$this->dtcKey} insert:{$k_val} error,code:{$errCode}"; $this->errMsg = $result->error_message(); $this->errFrom = $result->error_from(); return false; } // 若为自增型key,返回insert_id if (!isset($data[$key]) && $keyConfig['auto']) { $autoinc_id = $result->int_key(); return $autoinc_id; } return true; }
//设置ip/port $ttc_srv->set_address("192.168.214.62", "10009"); //设置访问码 $ttc_srv->set_accesskey("0000010284d9cfc2f395ce883a41d7ffc1bbcf4e"); //设置单次网络IO超时 $ttc_srv->set_timeout(5); $key = 100009; //构建 update 请求,即 对应下面填的参数 7 ,插入数据 $ttc_update = new tphp_ttc_request($ttc_srv, 7); $ret = $ttc_update->set_key($key); printf("update set key.ret:%d\n", $ret); //set函数是用来指定insert sql 语句中的set 字段名=value 的这样一个操作 $ttc_update->set_str("name", "9qwer"); $ttc_update->set_str("city", "fuzhou"); $ttc_update->set_str("descr", "huanying"); $ttc_update->set("age", 21); $ttc_result = new tphp_ttc_result(); $ret = $ttc_update->execute($ttc_result); printf("update execute end.ret:%d\n", $ret); if ($ret == 0) { //清空result对象 $ttc_result->reset(); //创建get 请求request对象, 4表示get操作 $ttc_get = new tphp_ttc_request($ttc_srv, 4); $ttc_get->set_key($key); //指定需要查询的字段 $ttc_get->need("name"); $ttc_get->need("city"); $ttc_get->need("age"); $ret = $ttc_get->execute($ttc_result); printf("get execute end.ret:%d\n", $ret);
$ttc_srv->int_key(); //设置表名 $ttc_srv->set_tablename("t_dtc_example"); //设置ip/port $ttc_srv->set_address("192.168.214.62", "10009"); //设置访问码 $ttc_srv->set_accesskey("0000010284d9cfc2f395ce883a41d7ffc1bbcf4e"); //设置单次网络IO超时 $ttc_srv->set_timeout(5); $key = 100009; //构建Insert 请求,插入数据 $ttc_insert = new tphp_ttc_request($ttc_srv, 6); $ret = $ttc_insert->set_key($key); printf("insert set key.ret:%d\n", $ret); //set函数是用来指定insert sql 语句中的set 字段名=value 的这样一个操作 $ttc_insert->set("uid", $key); $ttc_insert->set_str("name", "tttt"); $ttc_insert->set_str("city", "sadsa"); $ttc_insert->set_str("descr", "hkjhsakjfhkjdsah"); $ttc_insert->set("age", 30); $ttc_result = new tphp_ttc_result(); $ret = $ttc_insert->execute($ttc_result); printf("insert execute end.ret:%d\n", $ret); if ($ret == 0) { //清空result对象 $ttc_result->reset(); //创建get 请求request对象 $ttc_get = new tphp_ttc_request($ttc_srv, 4); $ttc_get->set_key($key); //指定需要查询的字段 $ttc_get->need("name");