dpps0715 2011-02-05 15:10
浏览 79
已采纳

将PHP变量设置为MySQL行中的最新值

I searched old questions, but couldn't find an answer or wrap my head around anything relevant (new to PHP and MySQl so take it easy)!

I have a MySQL table with the columns: id, subid, name; I ran this query:

CREATE TABLE mytable (id int(999999) NOT NULL auto_increment,subid varchar(99) NOT NULL,name varchar(99) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))

From the value in subid on the latest row I want to set to $latestsubid, this is what I've managed to throw together:

    <?
    $user="me";
    $password="blah";
    $database="mydb";
    $host="localhost";
    mysql_connect($host,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT subid FROM mytable";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    ?>

If anyone could give me a hand it'd be great!

  • 写回答

3条回答 默认 最新

  • douyi4544 2011-02-05 15:24
    关注

    I'm not sure if i understand what you want to achieve, but if you want to get the "subid" column value from the table row with the highest "id" value you can do:

    $query="SELECT subid FROM mytable ORDER BY id DESC LIMIT 1";
    $result=mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $latest_subid = $row['subid'];
    

    This will sort the table descending on id (so the first row returned is the one with the highest id) and then just limit your result to 1 row,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部