0

I am attempting to dumping a Microsoft SQL (MSSQL) database to a file similar to how i would dump from a MySQL database using php:

$cmd = "mysql --user=".$db_user." --password=".$db_password." --host=".$hostname." --database=".$database." --execute='SELECT * FROM ".$database." '> file.txt";
exec($cmd);

But i am trying to figure out the similarities in the command line as to how it is done above with MySQL What is the proper command line syntax to execute this? Thanks.

3
  • There's sqlcmd: databasejournal.com/features/mssql/article.php/3654176/… Commented Mar 21, 2012 at 18:23
  • That was just an example code, i don't want to limit it actually. Sorry about that Commented Mar 21, 2012 at 18:25
  • P.S. I know that this question is old but please sanitize the arguments with escapeshellarg($db_user) and same for password and hostname or this could create security nightmares in your app :) even if this is just an example. Let's give a good example if possible. Commented Dec 20, 2024 at 13:51

1 Answer 1

1

First, you need to write and test your BACKUP command in TSQL. Second, you need to execute it, either using sqlcmd.exe (Windows only) or by connecting directly to the database with whatever MSSQL drivers you use in PHP.

Since the SQL Server documentation is very complete and has numerous examples, you should be able to write the commands yourself. If you still have problems, please post what you have tried so far and exactly what you can't figure out.

Finally, you should always mention the version and edition of SQL Server, because it's often important to know when answering questions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.