drqj8605 2012-06-06 10:46
浏览 47

Zend PHP - sqlsrv insert和带有fetchOne的Select_Scope()

I am creating a website using PHP and Zend Framework. I am uisng the sqlsrv adaptor to talk to my database. I am trying to write an insert query and also store the ID of the new row. I have written an SQL query like this:

$db = Zend_Db_Table::getDefaultAdapter();               
$id = $db->fetchOne("
    INSERT INTO    MyTable
    (Title, CreatedBy, Created, LastUpdatedBy, LastUpdated)
    SELECT  'MY Title',
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["Created"]."', 120),
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["LastUpdated"]."', 120)
    FROM    UserTable
    WHERE   UserName = '".$this->details["CreatedBy"]."'
    SELECT  CAST(SCOPE_IDENTITY() AS int) AS Id"
);

but it generates this error:

'Zend_Db_Statement_Sqlsrv_Exception' with message 'The active result for the query contains no fields.

How can I avoid this error and run this query and retreive the inserted row Id?

  • 写回答

1条回答 默认 最新

  • dongyi8795 2012-06-06 10:50
    关注

    You seem to be using the wrong method here.

    You should be using the insert() method of Zend_Db_Table:-

    $table = new Zend_DB_Table(array('name' => 'mytable', primary => 'myPrimaryKey'));
    $data = array()// of data to be inserted
    $id = $table->insert($data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭