简体   繁体   English

OUTPUT子句在PHP中返回

[英]OUTPUT clause get a return in PHP

After doing some research I think I need the Output clause. 经过研究后,我认为我需要Output子句。 Essentially I am taking the below SQL and inserting into the specified table when I receive the location of a file I am uploading to the server. 本质上,当我收到要上传到服务器的文件的位置时,我将使用下面的SQL并将其插入到指定的表中。 When I upload into the table the ID is an auto increment field and the primary key. 当我上传到表格中时,ID是一个自动递增字段和主键。

$conn = mysqli_connect($DBHOSTmy, $DBuser, $DBpass, $DBmy) or die("An error occurred connecting to the database " . mysqli_error($conn));

$query = "INSERT INTO ebwf (src,loc,iq,wq,pq) OUTPUT Inserted.id, Inserted.src, Inserted.loc, Inserted.iq, Inserted.wq, Inserted.pq VALUES ('" . $source . "','" . $finalPdf . "','y',0,0);";

echo $query;

$result = $conn->query($query); 

echo $result->num_rows;


$conn->close();

When this runs I get a return of INSERT INTO ebwf (src,loc,iq,wq,pq) OUTPUT Inserted.src, Inserted.loc, Inserted.iq, Inserted.wq, Inserted.pq VALUES ('m','scan/WF_153_140812113520.pdf','y',0,0); 运行此命令时,我将返回INSERT INTO ebwf (src,loc,iq,wq,pq) OUTPUT Inserted.src, Inserted.loc, Inserted.iq, Inserted.wq, Inserted.pq VALUES ('m','scan/WF_153_140812113520.pdf','y',0,0); , but I get no return of number rows. ,但没有返回数字行。

I really just need the ID right this minute of the inserted row, but if we can get all of these fields that would be awesome. 我确实只是在插入行的这一分钟才需要ID,但是如果我们能够获得所有这些字段,那就太好了。

I pretty much copied the usage of the OUTPUT clause from a few different places, but I don't see what I'm doing wrong to get no return... 我几乎从几个不同的地方复制了OUTPUT子句的用法,但是我看不到我做错了什么而没有回报...

I'm trying to do some research while writing this as I have not had good response rates because people think I'm lacking it so I also found: How do I use an INSERT statement's OUTPUT clause to get the identity value? 我试图在撰写本文时做一些研究,因为我没有很好的答复率,因为人们认为我缺乏答复率,所以我也发现: 如何使用INSERT语句的OUTPUT子句获取标识值? ... I changed my query only to: ...我只将查询更改为:

$query = "DECLARE @OutputTbl TABLE (id INT, src VARCHAR, loc VARCHAR, iq INT, wq INT, pq INT);

INSERT INTO ebwf (src,loc,iq,wq,pq) OUTPUT Inserted.id, Inserted.src, Inserted.loc, Inserted.iq, Inserted.wq, Inserted.pq INTO @OutputTbl(id, src, loc, iq, wq, pq) VALUES ('" . $source . "','" . $finalPdf . "','y',0,0);";

I sadly still get nothing.. Hopefully this will give enough info as to what I should do next. 可悲的是我仍然一无所获。。希望这会提供足够的信息,说明我下一步应该做什么。

@cmorrissey provided a great solution... @cmorrissey提供了一个很好的解决方案...

$last_insert_id = $conn->insert_id;

This returns the last inserted id (I think primary key that is auto incremented is the most correct explanation.) This is better to use than OUTPUT. 这将返回最后插入的ID(我认为自动递增的主键是最正确的解释。)这比OUTPUT更好用。 I'm not sure why OUTPUT is incorrect to use as I have seen it so many places and this only once from the user. 我不确定为什么使用OUTPUT是不正确的,因为我在很多地方都看到过它,而这仅来自用户一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM