我有一个存储过程,可以进行备份,然后在出现错误或不是这样的情况下返回1或0:
Create procedure [dbo].[sp_IWBackup] as begin declare @route varchar(500), @answer int = 0 set nocount on set @route = 'I:\route'+(replace((replace(convert(varchar, getdate(), 21),':',';')),'.',';'))+'Full.bak' set @answer = 1 begin try backup database databasename to disk = @route end try begin catch set @answer = 0 end catch select @answer as answer end
在sql中,它可以正常工作并返回@answer的正确值。
在php中,当我尝试获取查询的执行时,它什么也找不到。
public function ExecuteSelectAssoc($sth) { $r=array('data'=>false, 'error'=>false, 'r'=>array()); try { $sth->execute(); while ($row=$sth->fetch(PDO::FETCH_ASSOC)) { //error here $r['data'] = true; $keys = array_keys($row); $tmp = array(); foreach($keys as $key) { $tmp[$key] = $row[$key]; } array_push($r['r'], $tmp); } } catch (PDOException $e) { $r['error']=true; $r['r'] = $e->getMessage(); } return $r; }
PDO的属性
public function connectSqlSrv(){ try{ $dbCnx = new PDO("sqlsrv:Server=$this->server;Database=$this->db", $this->usr, $this->psw); $dbCnx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $dbCnx; } catch(PDOException $e){ echo $e; die(); return null; } }
我得到这个错误:
r:“ SQLSTATE [IMSSP]:查询的活动结果不包含任何字段。”
我希望:答案:1
说明:
错误的原因是BACKUP DATABASE返回参考消息,并且您的存储过程具有多个结果集。我用测试脚本重现了您的错误(尽管在您的问题中我看不到您如何准备存储过程)。您可以尝试以下解决方案之一(我编写了简单的脚本,可以轻松地在函数中实现这些脚本)。
BACKUP DATABASE
调用PDOStatement :: nextRowset来获取每个结果集(在这种情况下,需要再进行两次调用)
OUTPUT在您的存储过程中使用参数。在这种情况下,您需要获取所有结果集,然后再获取输出参数的值。
OUTPUT
解决方案1:
存储过程:
create procedure [dbo].[sp_IWBackup] as begin declare @route varchar(500), @answer int = 0 set nocount on set @route = 'I:\route'+(replace((replace(convert(varchar, getdate(), 21),':',';')),'.',';'))+'Full.bak' set @answer = 1 begin try backup database databasename to disk = @route end try begin catch set @answer = 0 end catch select @answer as answer end
PHP:
<?php $server = 'server\instance,port'; $database = 'database'; $uid = 'uid'; $pwd = 'pww'; try { $dbh = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch( PDOException $e ) { die("Error connecting to SQL Server. ".$e->getMessage()); } try { $sql = "{CALL sp_IWBackup}"; $stmt = $dbh->prepare($sql); $stmt->execute(); $stmt->nextRowset(); $stmt->nextRowset(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { foreach ($row as $key => $value) { echo $key.": ".$value."<br>"; }; } } catch( PDOException $e ) { die( "Error executing stored procedure: ".$e->getMessage()); } $stmt = null; $dbh = null; ?>
解决方案2:
create procedure [dbo].[sp_IWBackupOut] @answer int OUTPUT as begin declare @route varchar(500) set nocount on set @route = 'I:\route'+(replace((replace(convert(varchar, getdate(), 21),':',';')),'.',';'))+'Full.bak' set @answer = 1 begin try backup database databasename to disk = @route end try begin catch set @answer = 0 end catch end
<?php $server = 'server\instance,port'; $database = 'database'; $uid = 'uid'; $pwd = 'pww'; try { $dbh = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch( PDOException $e ) { die("Error connecting to SQL Server. ".$e->getMessage()); } try { $sql = "{CALL sp_IWBackupOut (?)}"; $stmt = $dbh->prepare($sql); $answer = -1; $stmt->bindParam(1, $answer, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE); $stmt->execute(); do { } while ($stmt->nextRowset()); echo "answer: ".$answer; } catch( PDOException $e ) { die( "Error executing stored procedure: ".$e->getMessage()); } $stmt = null; $dbh = null; ?>