dsd57259 2012-08-24 16:15
浏览 62
已采纳

插入一个查询后获取数据库行

At the moment I have a script where I add data to the database.

Once the data is entered I would like to get the get the row straight away.

For example if I have a query like this where I have two seperate:

    $sql = "INSERT INTO  table SET columnA '".$this->db->escape($columnA)."'";
    $query = $this->db->query($sql);
    $sql = "SELECT * FROM table";
    $query = $this->db->query($sql);
    return $query->db->row;

I want to be able to make get that database row instantly after inserting it. Will I have to make a whole new query or is there a quicker way? I am using OpenCarts API if that helps.

Thanks Peter

  • 写回答

1条回答 默认 最新

  • duanlou7910 2012-08-24 16:20
    关注
    INSERT INTO table (a,b,c) VALUES ('a','b','c');
    SELECT * FROM table WHERE your_table_primary_key = LAST_INSERT_ID();
    

    PHP:

    $sql  = "INSERT INTO  table SET columnA '".$this->db->escape($columnA)."';";
    $sql .= "SELECT * FROM table WHERE your_table_primary_key = LAST_INSERT_ID();"
    $query = $this->db->query($sql);
    return $query->db->row;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效