如何解决php PDO :: FETCH_ASSOC在存储过程中备份后未检测到选择
错误的原因是BACKUP
DATABASE
返回参考消息,并且您的存储过程具有多个结果集。我用测试脚本重现了您的错误(尽管在您的问题中我看不到您如何准备存储过程)。您可以尝试以下解决方案之一(我编写了简单的脚本,可以轻松地在函数中实现这些脚本)。
-
调用PDOStatement :: nextRowset来获取每个结果集(在这种情况下,需要再进行两次调用)
存储过程:
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;
?>
存储过程:
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:
<?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;
?>
解决方法
我有一个存储过程,可以进行备份,然后在出现错误或不是这样的情况下返回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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。