首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动增加从数据库获得的VARCHAR值?

问自动增加从数据库获得的VARCHAR值?
EN

Stack Overflow用户
提问于 2017-09-21 11:46:22
回答 4查看 542关注 0票数 0

我从DB获得DRSYSCODE的值,现在我需要在插入时自动增加这个值

我现在在插入时得到这种格式的值MOB000571,我需要增加到MOB000572,因为这是使用下面的代码,但无法实现它,作为回报,得到的值只有1,请在这里帮助我

代码语言:javascript
复制
$sql1="SELECT DRSYSCODE FROM DOCTORDETAILS  ORDER BY DRSYSCODE DESC LIMIT 1";
    $query=mysql_query($sql1);
    if (!$sql1) { // add this check.
    die('Invalid query: ' . mysql_error());
    }
    $output_array2=array();
    while($row=mysql_fetch_assoc($query))
    {
    $ids=$row['DRSYSCODE'];

    }
    echo $ids;
    if($ids){
        $su=1;
        echo $num =$ids+$su;
        $unique=$num;
    }

    echo $unique;
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-09-21 12:02:38

不要使用$num =$ids+$su;,而要使用以下内容:

代码语言:javascript
复制
$num = 'MOB' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);
票数 2
EN

Stack Overflow用户

发布于 2017-09-21 11:58:08

就像这样,

代码语言:javascript
复制
$pieces = explode("MOB", $string);
$autoinc_add = 1+$pieces[1];
$your_latest_autoincrement_value = str_pad($autoinc_add, 6, '0', STR_PAD_LEFT);
echo $your_latest_autoincrement_value;
票数 3
EN

Stack Overflow用户

发布于 2017-09-21 12:24:10

您的问题是,您正试图将1,一个整数添加到文本字符串中。您需要将文本字符串拆分为文本部分,数字部分首先分解为两个独立的变量(除非“暴徒”总是前三个字符,或者DRSYSCODE中始终有三个字符,后面跟着六个数字),然后将1添加到数字部分,然后在插入之前再次将它们连接在一起。例如,如果DRSYSCODE始终为AAAxxxxxx (“A”为字符,“x”为数字),则可以执行以下操作:

代码语言:javascript
复制
if($ids){
    $alpha = substr($ids, 0, 3); // Get the first three characters and put them in $alpha
    $numeric = substr($ids, 3, 6); // Get the following six digits and put them in $numeric
    $numeric++; // Add one to $numeric
    $numeric = str_pad($numeric, 6, '0', STR_PAD_LEFT); // Pad $numeric with leading zeros
    $newids = $alpha.$numeric; // Concatenate the two variables into $newids
    }

我希望这能帮到你。我在您的代码中看到了可以简化它的其他地方,但这不是您问题的一部分。祝你一切顺利。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46343127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档