duanpo2813 2012-08-17 22:51
浏览 353
已采纳

获取数据库表中列的最大值

I'm trying to get the max value of a column in a database table.

This is my PHP:

$st = mysql_fetch_assoc(mysql_query("SELECT max(`version`) FROM `remembers` WHERE id='$id'"));
$version = $st['version'] + 1;

So that should get the current version of id and then update it but adding 1 to it. But $st['version'] seems to be returning 0 when in the database the current highest is 1

Am I doing this wrong? Is there a better way to archive this?

  • 写回答

2条回答 默认 最新

  • douji5397 2012-08-17 22:52
    关注

    You have to alias the MAX() selection in SQL in order to reference it in PHP:

    SELECT max(`version`) AS version FROM ...
    

    Also, mysql_fetch_assoc returns an array of arrays, so you cannot simply reference $st['version'] directly. Instead, you can change your PHP to:

    $st = mysql_fetch_row(mysql_query("SELECT max(`version`) AS version FROM `remembers` WHERE id='$id'"));
    $version = $st[0] + 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作