从PHP调用MySql存储过程(PDO)
我正在尝试使用PDO连接执行一个MySQL存储过程,我尝试了几乎所有的方法,但无法执行它 SP将仅插入和更新。以下是我迄今为止尝试的代码从PHP调用MySql存储过程(PDO),php,mysql,sql,stored-procedures,pdo,Php,Mysql,Sql,Stored Procedures,Pdo,我正在尝试使用PDO连接执行一个MySQL存储过程,我尝试了几乎所有的方法,但无法执行它 SP将仅插入和更新。以下是我迄今为止尝试的代码 $config = require('protected/config/main.php'); try { $db_adat = new PDO($config['components']['db']['connectionString'], $config['components'] ['db']['username'], $config['compo
$config = require('protected/config/main.php');
try
{
$db_adat = new PDO($config['components']['db']['connectionString'], $config['components'] ['db']['username'], $config['components']['db']['password']);
$result= $db_adat->prepare('CALL pdv()');
$a = $result->execute();
$a->fetchAll(PDO::FETCH_ASSOC);
我尝试了使用only fetch()、only fetchAll()、fetchObject()、fetch(PDO::fetch_ASSOC)、fetchAll(\PDO::fetch_ASSOC),但总是出现以下错误
Fatal error: Call to a member function fetchAll() on a non-object in D:\ADAT_System\www\test\protected\controllers\ExportPDVController.php on line 35
我还尝试使用query()而不是execute(),但这也不起作用
我还尝试在SP中添加(select*)语句,并尝试使用上述所有“fetch”选项,但得到了相同的错误
SP需要7分钟才能完成,但都立即出错,所以我猜它从未运行过SP
我也试着跟着
$result= $this->$db_adat->prepare("CALL pdv()");
$result->execute();
但我得到了以下错误:
Object of class PDO could not be converted to string
我没有在SP中传递任何参数,只是一个简单的调用。如果需要更多信息,请告诉我。这部分代码是错误的
$result= $db_adat->prepare('CALL pdv()');
$a = $result->execute();
$a->fetchAll(PDO::FETCH_ASSOC);
因为execute()
在成功或失败时返回布尔值
您不能使用它来获取。以下是使用适当变量名的正确方法:
$stmt= $db_adat->prepare('CALL pdv()');
$success = $stmt->execute();
if($success){
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}else{
echo 'failed to run sp';
}
您必须使用nextRowset()
函数,因为没有它的下一条记录是“空”的
<?php
if(isset($_POST) && !empty($_POST)){
$SearchPO = (($_POST)['SearchPO']);
}
$stmt = $pdo->prepare("CALL spPO(?)");
$stmt->bindParam(1, $SearchPO, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT|PDO::ATTR_EMULATE_PREPARES, 4000);
$stmt->execute();
do {
$result = $stmt->fetchAll();
} while ($stmt->nextRowset() && $stmt->columnCount());
?>