用户的增删改查
THINKPHP兄弟连李文凯老师视频笔记:12用户的增删改查
Reply
用户的增删改查
$user = M(‘user’);
find 返回一维数组
select 将该表中的所有信息读取出来
findAll 二维数组
$list = $user->find();
//连贯操作,这里先忽略,因为我觉得不实用
myisam表级锁
innodb行级锁
<?php printf(' memory usage: %01.2f MB', memory_get_usage()/1024/1024); /* * sortBreakProcessor * example: * $serial = array('ab001','ab002','ab003','ab004','ab006','ab008','ab009','ab0010'); * $start = 4; * $length = 1; * return array( array('ab001','ab004'), array('ab006','ab006'), array('ab008','ab0010'), ); copy right http:://qqmaster.info/ */ $serial = array('ab001','ab002','ab003','ab004','ab006','ab008','ab009','ab0010'); $start = 4; $length = 1; $re = sortBreakProcessor($serial,$start,$length); var_dump($re); function sortBreakProcessor($serial,$start,$length) { if(!is_array($serial)) { return false; } if(!isset($serial[0]) && empty($serial[0])) { return false; } $prefix = substr($serial[0], 0,$start); $val = ''; foreach ($serial as $k=>$v) { $val[$k] = substr($v, $start,$length); } //print_r($val); //Bubble sort $val = paixu($val); //print_r($val); //get 1,4,6,6,8,10 $resultArr[] = $val[0]; foreach ($val as $k=>$v) { if($val[$k]+1<$val[$k+1]) { $resultArr[] = $val[$k]; $resultArr[] = $val[$k+1]; } } $resultArr[] = end($val); //print_r($resultArr); foreach ($resultArr as $k=>$v) { if($k%2==0) { $return[] = array($prefix.$resultArr[$k],$prefix.$resultArr[$k+1]); } } return $return; } //Bubble sort function paixu($str) { $n=count($str); for ($i=0;$i<$n;$i++) { for ($j=$n-2;$j>=$i;$j--) { if($str[$j+1]<$str[$j]) { $tmp = $str[$j+1]; $str[$j+1]=$str[$j]; $str[$j]=$tmp; } } } return $str; }
<?php /* * use for to transfer card serial from a to b,but the serial a have is like this ab-cde-000000001 to ab-cde-000000009 * when i want transfer (ab-cde-000000001 to ab-cde-000000003) or (ab-cde-000000003 to ab-cde-000000005) or (ab-cde-000000006 to ab-cde-000000009) to b * so there is a algorithm,see the function transCardById * db struct:id aid startSerial endSerial time * copy right http://qqmaster.info */ function transCardById($db, $startSerial,$endSerial,$serialId,$accountId, $newAccountId) { $result = false; $serialInfo = test::getSerialById2($db,$serialId); if(!empty($serialInfo)) { //echo '1';echo '<br />'; $num = '%1$09d'; $prefixe = test::getSerialPrefix($startSerial); $startSerialSuffix = test::getSerialSuffix($startSerial); $endSerialSuffix = test::getSerialSuffix($endSerial); $serialInfoStartSerialSuffix = test::getSerialSuffix($serialInfo['serialNumStart']); $serialInfoEndSerialSuffix = test::getSerialSuffix($serialInfo['serialNumEnd']); //echo $startSerialSuffix.'--'.$endSerialSuffix.'--'.$serialInfoStartSerialSuffix.'--'.$serialInfoEndSerialSuffix.'--'; // 1,5 if($serialInfoStartSerialSuffix==$startSerialSuffix) { //echo '2';echo '<br />'; if($serialInfoEndSerialSuffix==$endSerialSuffix) { //echo '3';echo '<br />'; // 1,5 // update accountId To newAccountId $result = test::updateSerialById($db,$serialId,$newAccountId); } elseif($serialInfoEndSerialSuffix>$endSerialSuffix) { //echo '4';echo '<br />'; // 1,3 // update start serial to 4 // add a record with newAccountId,serial from 1 to 3 $result = test::updateSerialById($db,$serialId,'',$prefixe.sprintf($num,($endSerialSuffix+1))); if($result==true) { //echo '5';echo '<br />'; $result = test::addCard($db, $startSerial, $endSerial, $newAccountId); } } } elseif($serialInfoStartSerialSuffix<$startSerialSuffix) { //echo '6';echo '<br />'; if($serialInfoEndSerialSuffix==$endSerialSuffix) { //echo '7';echo '<br />'; // 3,5 // update end serial to 2 // add a record with newAccountId,serial from 3 to 5 $result = test::updateSerialById($db,$serialId,'','',$prefixe.sprintf($num,($startSerialSuffix-1))); if($result==true) { //echo '8';echo '<br />'; $result = test::addCard($db, $startSerial, $endSerial, $newAccountId); } } elseif($serialInfoEndSerialSuffix>$endSerialSuffix) { //echo '9';echo '<br />'; // 3,4 // update end serial to 2 // add a record with accountId ,serial from 5 to 5 // add a record with newAccountId ,serial from 3 to 4 $result = test::updateSerialById($db,$serialId,'','',$prefixe.sprintf($num,($startSerialSuffix-1))); if($result==true) { //echo '10';echo '<br />'; $result = test::addCard($db, $prefixe.sprintf($num,($endSerialSuffix+1)), $serialInfo['serialNumEnd'], $accountId); if($result==true) { //echo '11';echo '<br />'; $result = test::addCard($db, $startSerial, $endSerial, $newAccountId); } } } } } return $result; } class test { static public function checkSerialMatch($startSerial,$endSerial) { $result = false; $checkStartSerialRight = self::checkSerialFormat($startSerial); $checkendSerialRight = self::checkSerialFormat($endSerial); if($checkStartSerialRight==true&&$checkendSerialRight==true) { //echo '30'; $startSerialPrefix = self::getSerialPrefix($startSerial); $endSerialPrefix = self::getSerialPrefix($endSerial); if($startSerialPrefix==$endSerialPrefix) { $startSerial = preg_replace('/^'.$startSerialPrefix.'/','',$startSerial); $endSerial = preg_replace('/^'.$endSerialPrefix.'/','',$endSerial); $startSerialNum = preg_replace('/^0*/','',$startSerial); $endSerialNum = preg_replace('/^0*/','',$endSerial); if($startSerialNum<=$endSerialNum) { $result = true; } } } return $result; } static public function checkSerialFormat($serial) { if(strlen($serial)!=16) { //echo '310'; return false; } // $serialPrefix = substr($serial,0,7); // $serialPrefixArr= explode('|', SERIAL_PREFIX); // if(!in_array($serialPrefix, $serialPrefixArr)) // { // //echo '311'; // return false; // } $serialNum = substr($serial,-9); if(!is_numeric($serialNum)) { //echo '312'; return false; } return true; } static public function getSerialPrefix($serial) { if(strlen($serial)!=16) { return false; } $serialPrefix = substr($serial,0,7); return $serialPrefix; } static public function getSerialSuffix($serial,$removeZeroFlag=true) { $return = false; if(strlen($serial)==16) { $serialSuffix = substr($serial,-9); if($removeZeroFlag=true) { $serialSuffix = preg_replace('/^0*/', '', $serialSuffix); } $return = $serialSuffix; } return $return; } }
this code is written is by myself,to check serial is match,thanks
<?php /* * check serial match,like give two serial:ab-cde-000000123,ab-cde-000000234 * copy right http://qqmaster.info/ */ $test = test::checkSerialMatch('ab-cde-000000123','ab-cde-000000124'); var_dump($test); $test = test::checkSerialMatch('ab-cde-000000123','ab-cde-000000122'); var_dump($test); $test = test::checkSerialMatch('ab-cde-000000123','ab-cde-000000123'); var_dump($test); class test { static public function checkSerialMatch($startSerial,$endSerial) { $result = false; $checkStartSerialRight = self::checkSerialFormat($startSerial); $checkendSerialRight = self::checkSerialFormat($endSerial); if($checkStartSerialRight==true&&$checkendSerialRight==true) { //echo '30'; $startSerialPrefix = self::getSerialPrefix($startSerial); $endSerialPrefix = self::getSerialPrefix($endSerial); if($startSerialPrefix==$endSerialPrefix) { $startSerial = preg_replace('/^'.$startSerialPrefix.'/','',$startSerial); $endSerial = preg_replace('/^'.$endSerialPrefix.'/','',$endSerial); $startSerialNum = preg_replace('/^0*/','',$startSerial); $endSerialNum = preg_replace('/^0*/','',$endSerial); if($startSerialNum<=$endSerialNum) { $result = true; } } } return $result; } static public function checkSerialFormat($serial) { if(strlen($serial)!=16) { //echo '310'; return false; } // $serialPrefix = substr($serial,0,7); // $serialPrefixArr= explode('|', SERIAL_PREFIX); // if(!in_array($serialPrefix, $serialPrefixArr)) // { // //echo '311'; // return false; // } $serialNum = substr($serial,-9); if(!is_numeric($serialNum)) { //echo '312'; return false; } return true; } static public function getSerialPrefix($serial) { if(strlen($serial)!=16) { return false; } $serialPrefix = substr($serial,0,7); return $serialPrefix; } static public function getSerialSuffix($serial,$removeZeroFlag=true) { $return = false; if(strlen($serial)==16) { $serialSuffix = substr($serial,-9); if($removeZeroFlag=true) { $serialSuffix = preg_replace('/^0*/', '', $serialSuffix); } $return = $serialSuffix; } return $return; } }