<?php
/*
* 创建人:小超
*
* 创建时间: 2006-12-25
* 修改时间:
* 功能:通过php方法来实现mysql的sequence功能
* 需创建一个表:
* create table sequence
* (
* id int not null primary key,
* table_name varchar(32)
* )
*/
class Sequence
{
function getNextValue($conn, $seqname, $cycle = '', $max,$start=0,$increment=1)
{
$resid = -1;
$sql = "select id from sequence where table_name='" . $seqname."'";
$reslut = mysql_query($sql, $conn);
$sql = "update sequence set id=last_insert_id(id+".$increment.") where table_name='" . $seqname."'";
if ($row = mysql_fetch_array($reslut))
{
$resid = $row['id'];
}
else
{
$sql="insert into sequence (id,table_name) values($start,'$seqname')";
@mysql_query($sql);
$resid=$start;
}
echo $sql;
if ($cycle != '')
{
if (((int) $resid) >= (pow(10, $max) - 1))
{
$sql = "update sequence set id=$start where table_name='$seqname'" ;
}
}
@ mysql_query($sql, $conn);
@ mysql_free_result($reslut);
return $resid;
}
?>
有不足的地方望大侠支持,小弟洗耳恭听.
/*
* 创建人:小超
*
* 创建时间: 2006-12-25
* 修改时间:
* 功能:通过php方法来实现mysql的sequence功能
* 需创建一个表:
* create table sequence
* (
* id int not null primary key,
* table_name varchar(32)
* )
*/
class Sequence
{
function getNextValue($conn, $seqname, $cycle = '', $max,$start=0,$increment=1)
{
$resid = -1;
$sql = "select id from sequence where table_name='" . $seqname."'";
$reslut = mysql_query($sql, $conn);
$sql = "update sequence set id=last_insert_id(id+".$increment.") where table_name='" . $seqname."'";
if ($row = mysql_fetch_array($reslut))
{
$resid = $row['id'];
}
else
{
$sql="insert into sequence (id,table_name) values($start,'$seqname')";
@mysql_query($sql);
$resid=$start;
}
echo $sql;
if ($cycle != '')
{
if (((int) $resid) >= (pow(10, $max) - 1))
{
$sql = "update sequence set id=$start where table_name='$seqname'" ;
}
}
@ mysql_query($sql, $conn);
@ mysql_free_result($reslut);
return $resid;
}
?>
有不足的地方望大侠支持,小弟洗耳恭听.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货