How to return max(id) in pdo

喜你入骨 提交于 2020-04-30 07:42:21

问题


I am trying to return the last id and increment it to put in the value attribute. I can make it work with old mysql but not with the PDO. I am learning (I think) PDO but it is not making sense.

Below is the code.

    <td><label for="invNum"></label>Invoice</td>
    <?php
    $stmt = $dbconn->prepare("SELECT max(invId) FROM     invoices");
    $stmt->execute();
    $invNum = $stmt->fetch();
    
    /* mysql_select_db("customers", $dbconn);
    $result = mysql_query("SELECT max(invId) FROM   invoices");
    if (!$result) {
    die('Could not query:' . mysql_error());
    }
    $invNum = mysql_result($result, 0);*/
    ?>
    <td><input type="text" name="invNum" id="invNum" 
    size="12" value="<?php echo ++$invNum; ?>" /></td>

回答1:


Try this :

  $stmt = $dbconn->prepare("SELECT MAX(invId) AS max_id FROM invoices");
  $stmt -> execute();
  $invNum = $stmt -> fetch(PDO::FETCH_ASSOC);
  $max_id = $invNum['max_id'];


来源:https://stackoverflow.com/questions/29332891/how-to-return-maxid-in-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!