cevincheung

看了一下 Typecho 是这么获取自增 ID 的

  •  
  •   cevincheung · May 22, 2015 · 2903 views
    This topic created in 4009 days ago, the information mentioned may be changed or developed.
    <?php
    /**
     * 取出最后一次插入返回的主键值
     *
     * @param resource $resource 查询的资源数据
     * @param mixed $handle 连接对象
     * @return integer
     */
     function lastInsertId($resource, $handle)
     {
         /** 查看是否存在序列,可能需要更严格的检查 */
        if ($handle->query('SELECT oid FROM pg_class WHERE relname = ' . $this->quoteValue($this->_lastTable . '_seq'))->fetchAll()) {
            return $handle->lastInsertId($this->_lastTable . '_seq');
        }
        return 0;
    }
    

    虽然returning id是可以直接返回ID,但是不兼容其他数据库(比如MySQL)。
    Typecho在建表的时候把seq就已经规范好规则。获取直接 tablename_seq就行。

    如果抛开这两种方案不说的话,怎么才能最简单、方便的获取自增ID?

    Supplement 1  ·  Sep 9, 2015
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3362 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 11:24 · PVG 19:24 · LAX 04:24 · JFK 07:24
    ♥ Do have faith in what you're doing.