Guest User

Untitled

a guest
Mar 4th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. <pre>
  2. <?PHP
  3. $sqlserver = '';
  4. $username = '';
  5. $password = '';
  6. $database = "";
  7. /*
  8. * writer.php version 2.0 - Major update
  9. * Takes stored procedure name, the input variables and an array of output variables
  10. * outputs php code necessary for getting results using php pdo
  11. *
  12. */
  13.  
  14. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  15.  
  16. $procedureName = 'stored_procedure_name';
  17.  
  18. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  19.  
  20. $connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password);
  21. $conn = sqlsrv_connect( $sqlserver, $connectionInfo);
  22. if( $conn ) {
  23. //echo "Connection established.<br />";
  24. }else{
  25. echo "Connection could not be established.<br />";
  26. die( print_r( sqlsrv_errors(), true));
  27. }
  28.  
  29. //construct the query.
  30. $query = "SELECT name, type_name(user_type_id) AS type, max_length, is_output FROM sys.parameters WHERE object_id = object_id('DBO.".$procedureName."')";
  31. $stmt = sqlsrv_query( $conn, $query);
  32. if( $stmt === false ) {
  33. die( print_r( sqlsrv_errors(), true));
  34. }
  35. // Make the first (and in this case, only) row of the result set available for reading.
  36.  
  37. $inputs = [];
  38. $outputs = [];
  39.  
  40. while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
  41. if($row['is_output']==1){
  42. if($row['type']=='varchar'){
  43. $type = 'varchar('.$row['max_length'].')';
  44. } else {
  45. $type = $row['type'];
  46. }
  47. $outputs[$row['name']] = $type;
  48. } else {
  49. $inputs[] = $row['name'];
  50. }
  51. }
  52.  
  53. $i = 1;
  54. $array = $inputs;
  55. $input = implode(', ', $inputs);
  56.  
  57. //nospace on the first line because it makes it easier to copy and paste ;)
  58. print 'if(!isset($pdo) || $pdo==null){'."\r\n";
  59. print ' $pdo = DB::connection()->getPdo();'."\r\n";
  60. print ' }'."\r\n";
  61. print ' //SET NOCOUNT ON; --you might need this... '."\r\n";
  62. print ' $stmt = $pdo->prepare("DECLARE @return int';
  63. if(count($outputs)>0){
  64. foreach($outputs as $var=>$type){
  65. print ', '.$var.' '.$type;
  66. }
  67. }
  68. print '; EXEC @return = ['.$procedureName.'] ';
  69. print implode(' = ?, ', $array);
  70. $comma = '';
  71. if(count($array)>0 && strlen($input)>0){
  72. print ' = ?';
  73. $comma = ', ';
  74. }
  75. if(count($outputs)>0){
  76. foreach($outputs as $var=>$type){
  77. print $comma.$var.' = '.$var.' OUTPUT';
  78. $comma = ', ';
  79. }
  80. }
  81. print '; SELECT @return AS N\'@returnVal\'';
  82. if(count($outputs)>0){
  83. foreach($outputs as $var=>$type){
  84. print ', '.$var.' AS N\''.$var.'\'';
  85. }
  86. }
  87. print ';");'."\r\n";
  88. if(count($array)>0 && strlen($input)>0){
  89. foreach($array as $item){
  90. print' $stmt->bindValue('.$i.', $'.str_replace('@p_', '', str_replace('@', '', $item)).' ); //'.str_replace('_', ' ', str_replace('@p_', '', $item))."\r\n";
  91. $i++;
  92. }
  93. }
  94. print ' $stmt->execute();'."\r\n";
  95. //print ' //TODO: Format response'."\r\n";
  96. print ' //Should have the resultset...'."\r\n";
  97. print ' $rows = $stmt->fetchAll( \PDO::FETCH_ASSOC );'."\r\n";
  98. print ' $stmt->nextRowSet();'."\r\n";
  99. print ' //Should have the select response, or if there are multiple resultsets, it\'ll be here.'."\r\n";
  100. print ' $return = $stmt->fetchAll( \PDO::FETCH_ASSOC );';
  101.  
  102. /*
  103. for returned variables:
  104. DECLARE @var type; SET @var = null; STATEMENT; SELECT @var AS N'var';
  105. ^-- And set the output var like this: @p_Service_Request_ID = @var OUTPUT
  106. */
Add Comment
Please, Sign In to add comment