sql server - Return Select * from PHP PDO Stored Procedure In MS SQL -
for future users: bottom of question contains corrected working code.
i know select * not best, in example, trying call stored procedure php , return entire result set can loop through array in code.
here current stored procedure:
use [hanoncs_askme] go set ansi_nulls on go set quoted_identifier on go set nocount on; go create procedure [hanoncs_hanoncs].[commentstemp] @questionid int begin begin transaction if object_id('#viewquestioncomments', 'u') not null drop table #viewquestioncomments; create table #viewquestioncomments ( commentid int default ((0)), userid int default ((0)), comment varchar(max) default '', datemodified smalldatetime, username nvarchar(200) default '', points int default ((0)) ); insert #viewquestioncomments ( commentid, userid, comment, datemodified ) select id, userid, comment, datemodified hanoncs_askme.hanoncs_hanoncs.comments postid=1 , status=1; update #viewquestioncomments set username = m.username #viewquestioncomments c left join hanoncs_securelogin.hanoncs_hanoncs.members m on m.id = c.userid; update #viewquestioncomments set points = ( select count(*) hanoncs_askme.hanoncs_hanoncs.commentvotes postid=c.commentid) #viewquestioncomments c; select * #viewquestioncomments; if @@error != 0 rollback transaction else commit transaction end in ms sql management studio, returns temp table want:
exec [hanoncs_hanoncs].[commentstemp] @questionid = 1 i calling in php with:
$stmt = $pdo->prepare('exec [hanoncs_hanoncs].[commentstemp] @questionid = ?'); $stmt->bindparam(1, $questionid, pdo::param_int); $stmt->execute(); $rows6 = $stmt->fetch(pdo::fetch_both); the error is:
pdoexception sqlstate[imssp]: active result query contains no fields. /
edit:for future users!!! working code below.
stored procedure:
use [hanoncs_askme] go set ansi_nulls on go set quoted_identifier on go alter procedure [hanoncs_hanoncs].[commentstemp] @questionid int output begin set nocount on; begin transaction if object_id('#viewquestioncomments', 'u') not null drop table #viewquestioncomments; create table #viewquestioncomments ( commentid int default ((0)), userid int default ((0)), comment varchar(max) default '', datemodified smalldatetime, username nvarchar(200) default '', points int default ((0)), avatar nvarchar(200) default '' ); insert #viewquestioncomments ( commentid, userid, comment, datemodified ) select id, userid, comment, datemodified hanoncs_askme.hanoncs_hanoncs.comments postid=1 , status=1; update #viewquestioncomments set username = m.username , avatar = m.avatar #viewquestioncomments c left join hanoncs_securelogin.hanoncs_hanoncs.members m on m.id = c.userid; update #viewquestioncomments set points = ( select count(*) hanoncs_askme.hanoncs_hanoncs.commentvotes postid=c.commentid) #viewquestioncomments c; select * #viewquestioncomments; if @@error != 0 rollback transaction else commit transaction end php:
$stmt = $pdo->prepare('exec [hanoncs_hanoncs].[commentstemp] @questionid = ?'); $stmt->bindparam(1, $questionid, pdo::param_int); $stmt->execute(); $rows = $stmt->fetchall(pdo::fetch_assoc);
try adding set nocount on; inside stored procedure (not above creating procedure).
add after begin , before begin transaction;
like so:
create procedure [hanoncs_hanoncs].[commentstemp] @questionid int begin set nocount on; begin transaction from looks me, result set contain row counts of affected rows first, actual data want back.
Comments
Post a Comment