简体   繁体   English

使用PDO在php中使用Mac运行MS SQL Server存储过程

[英]Running a MS SQL server stored procedure with a mac in php using PDO

I am trying to execute a stored procedure in an instance of ms sql server, using php from a mac. 我正在尝试使用Mac上的php在ms sql服务器的实例中执行存储过程。

I have recently bought a mac book pro and seem to be having real problems achieving this. 我最近购买了Macbook Pro,但在实现此功能时似乎遇到了实际问题。

I now have a connection established, which I know is working as if I change the password I get the message: 我现在已经建立了一个连接,我知道它正在工作,就像我更改密码一样,我收到消息:

"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[01002] Adaptive Server connection failed (severity 9)' in /Users/davidwhitwell/Sites/test/connect.php on line 6"

Correcting the password does not give me the error. 更正密码不会给我错误。

The script I am trying to run is: 我尝试运行的脚本是:

    $sql = $pdo->prepare("CALL [finance].[get_all_company_names]");
    $sql->execute();
    $results = $sql->fetchAll();
    var_dump($results);

The result returns an empty array. 结果返回一个空数组。 I know data exists and that the correct permissions are in place to execute the procedure as I can do this directly with sql server management studio. 我知道数据存在,并且正确的权限已执行该过程,因为我可以直接使用sql Server Management Studio执行此操作。

Since you mentioned that you are not able to retrieve any data, even with a simple select query, I suggest adding some error handling to your script. 由于您提到即使使用简单的选择查询也无法检索任何数据,因此我建议向脚本中添加一些错误处理。

First, I would add some exception handling to your general pdo object. 首先,我将向您的常规pdo对象添加一些异常处理。

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This will throw an exception when your query fails. 当查询失败时,这将引发异常。 Then wrap your execution in a try/catch statement 然后将执行结果包装在try / catch语句中

try {
  $sql = $pdo->prepare("CALL [finance].[get_all_company_names]");
  $sql->execute();
  $results = $sql->fetchAll();
  var_dump($results);
} catch (PDOException $e) {
  echo 'An error occurred: ' . $e->getMessage();
}

The sql and the pdo objects you created also have a method called errorInfo, that will sometimes display an error. 您创建的sql和pdo对象还具有一个称为errorInfo的方法,该方法有时会显示错误。

if($sql->execute()) {
  $results = $sql->fetchAll();
}
else {
  $error_info = $dbh->errorInfo();
  print_r($error_info);
}

Some good resources include these page 一些好的资源包括这些页面

EDIT 编辑

I believe the ultimate is that stored procedures in MS SQL are called using EXEC 我相信最终是使用EXEC调用MS SQL中的存储过程

  $sql = $pdo->prepare("EXEC [finance].[get_all_company_names]");

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

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