Calling stored procedure from PHP using PDO to MSSQL Server using INPUT Paramters

20,223

For some reason this works:

  $sth = $dbh->prepare("exec wcweb_UserInfo ?");
  $sth->bindParam(1, $name);
  $sth->execute();

  while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
    var_dump($result);
  }

I might be able to live with this. Anyone know why the other methods do not work? Is it a difference in the libraries?

Share:
20,223
Michael Fever
Author by

Michael Fever

hacker noun hack·er | \ˈha-kər \ Definition of hacker 1: one that hacks 2: an expert at programming and solving problems with a computer 3: a person who is inexperienced or unskilled at a particular activity a tennis hacker 4: a person who illegally gains access to and sometimes tampers with information in a computer system 1 & 2 could be used to describe me, but definitely not 3 or 4 :) @MichaelDeMutis

Updated on January 31, 2020

Comments

  • Michael Fever
    Michael Fever over 4 years

    This does not work:

      $dbh = new PDO("dblib:host=xxxx;dbname=xxx", "xxxxx", "xxxxx");
    
      $sth = $dbh->prepare("{exec wcweb_UserInfo(?)}");
      $sth->bindParam(1, $name);
      $sth->execute();
    
      while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
        var_dump($result);
      }
    

    This also does not work:

      $dbh = new PDO("dblib:host=xxxxx;dbname=xxxx", "xxxxx", "xxxx");
    
      $sth = $dbh->prepare("{call wcweb_UserInfo(?)}");
      $sth->bindParam(1, $name);
      $sth->execute();
    
      while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
        var_dump($result);
      }
    

    This DOES work:

      $dbh = new PDO("dblib:host=xxxxx;dbname=xxxx", "xxxxx", "xxxx");
    
      $sth = $dbh->prepare("exec wcweb_UserInfo @userid=?");
      $sth->bindParam(1, $name);
      $sth->execute();
    
      while($result = $sth->fetch(PDO::FETCH_ASSOC)) {
        var_dump($result);
      }
    

    I tried the above using 2 that did not work with and without the curly brackets, etc. I know some would say, well just do it the way it works? .. The problem is I am porting an exising application from an IIS Server using the sqlsrv_query library to a Linux server.

    All of the database calls in the app are written in functions that use this method: {call wcweb_UserInfo(?)} .. None of the parameter names are specified, so I would have to modify every database call to include the parameter names. I was under the impression that the PDO library for PHP5 can do those same kind of calls?

    Help! Is there something I am doing wrong or is it just that PDO can't make those kinds of calls?

  • Nate C-K
    Nate C-K over 10 years
    Sorry, before I missed the fact that that you were using SQL Server. SQL Server doesn't use parentheses for stored procedure calls.