2

I've been wracking my brain over this in trying to call sqlcmd in PHP (IIS/Windows) but I'm at a complete loss as to why it's not working.

Code:

 $sql = 'sqlcmd -S '.$serverName.' -d '.$databasename.' -U '.$userName.' -P '.$userPassword.' -q "exec GenerateInstallScript 123456890" -o C:\temp\sqlscripts.sql';
    shell_exec($sql);

The code runs but nothing gets generated in the

C:\temp

folder. I tried running it manually from start / run and it runs without any issues. I even tried just exec($sql), using full absolute path to sqlcmd, tried calling cmd.exe and then sqlcmd but so far nothing.

I added IUSR full and Users full permission to the folder as well just to make sure it's not permissions from IIS but still nothing.

I also used proc mon to see if that would tell me anything and I'm not seeing anything related to sqlcmd, cmd, or php.exe (that would lead me to a reason why - entries show up but just standard call entries).

I've done an extensive amount of searching about PHP and calling SQLCMD but it seems like most people call a batch file with the sqlcmd in it. I can't really do that here so any guidance will be appreciated!

5
  • Have you tried using absolute/full path to sqlcmd in the shell_exec function?
    – Latheesan
    Oct 7, 2014 at 13:20
  • $sql = 'C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE -S '.$serverName.' -d '.$databasename.' -U '.$userName.' -P '.$userPassword.' -q "exec GenerateInstallScript 123456890" -o C:\temp\sqlscripts.sql'; shell_exec($sql); </br> I ran the above and no output. I even tried using \\ and still nothing. Thanks!
    – jmc1919
    Oct 7, 2014 at 13:25
  • Have you tried to see what $sql contains and if it is correct? Have you tried the command that works on the command line as is in shell_exec?
    – OIS
    Oct 7, 2014 at 13:27
  • Have you checked you're not using safe mode? php.net/safe-mode
    – OIS
    Oct 7, 2014 at 13:29
  • I just debugged the code to get the data in $sql and I was able to run it via command. One thing of note is that when I did run it, UAC prompt came up. Could that be what's restricting this? EDIT: I checked and safe_mode is set to off.
    – jmc1919
    Oct 7, 2014 at 13:32

2 Answers 2

0

Here's a possible idea, use a batch script to have the work done for you like this:

sqlcmd.bat

@ECHO OFF
cd /D "C:\Program Files\Microsoft SQL Server\100\Tools\Bin\"
sqlcmd -S %1 -d %2 -U %3 -P %4 -q "exec GenerateInstallScript 123456890" -o C:\temp\sqlscripts.sql

Then call this batch script using php like this:

test.php

<?php

$serverName = 'xyz';
$databasename = 'xyz';
$userName = 'xyz';
$userPassword = 'xyz';

shell_exec("sqlcmd.bat $serverName $databasename $userName $userPassword");

echo 'Done';

?>
1
  • I went through and modified my code and add the .bat file to the root of my web directory and the sqlscripts.sql file wasn't created. Is it possible that it's permissions? Thanks for the help so far!
    – jmc1919
    Oct 7, 2014 at 13:41
0

I didn't find a solution online, so I thought I might port this in case anyone needs it...

Something is telling the php user that they are not allowed to specifically run "SQLCMD.EXE". I just didn;t get around to finding out what that is, so I went around it...

find, copy, and rename this ... SQLCMD.rll ... to this... ss_sql.rll

find, copy, and rename this ... SQLCMD.exe ... to this... ss_sql.exe

And it now works... for me at least :)

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.