Dumping Microsoft SQL database to file using command line - php

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.

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.

Related

Sqlite3 dump database through PHP

I want to be able to dump a whole sqlite database to a file (database.sql) so as to use it as a backup. The problem is that exec() in PHP doesn't seem to run correctly the ".dump" command as it gives only "COMMIT;" as a result and not the whole text:
$db = new SQLite3('checks_db.db');
$results=$db->exec('.dump');
$content=$results->fetchArray();
If I run it from sqlite3 it gives both the schema and the insert insrtuctions with the data.
Perhaps I should also mention that the database has 777 permissions.
Can anyone write an example of the syntax of the '.dump' command in php-sqlite3?
The SQLite database does not have a .dump command.
The sqlite3 command-line shell has a .dump command.
To be able to use it, you would have to execute that tool; something like this:
exec('sqlite3 /some/where/checks_db.db .dump', $output);
The easiest way to make a backup would be to copy the database file itself, but using the shell's .backup command would be safer.

Importing huge Database into local server

Is there any way I can Import a huge database into my local server.
The database is of 1.9GB and importing it into my local is causing me a lot of problems.
I have tried sql dumping and was not successful in getting it in my local and have also tried changing the Php.ini settings.
Please let me know if there is any other way of getting this done.
I have used BigDump and also Sql Dump Splitter but I am still to able to find a solution
mysql -u #username# -p #database# < #dump_file#
Navigate to your mysql bin directory and login to your mysql
Select the database
use source command to import the data
[user#localhost] mysql -uroot -hlocalhost // assuming no password
[user#localhost] use mydb // mydb is the databasename
[user#localhost] source /home/user/datadump.sql
Restoring a backup of that size is going to take a long time. There's some great advice here: http://vitobotta.com/smarter-faster-backups-restores-mysql-databases-with-mysqldump/ which essentially gives you some additional options you can use to speed up both the initial backup and the subsequent restore.

Using MySQL "SOURCE /path/to/file.sql" with PDO

This works when I run it from the mysql cli client but not when I run it through PDO, does anyone know why?
source is a command that allow you to execute list of SQL in mysql CLI,
but PDO is a database driver library (api) that compiled into PHP,
both are in different domain
Details of source :-
https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
http://dev.mysql.com/doc/refman/5.0/en/batch-mode.html
You can either :-
exec("/PATH/mysql < $file");
Or break each line of SQL (inside the file),
execute one-by-one each in PDO

How can i use php to export a mysql table

I am trying to export just the rows of a mysql table without the table information to an xml file. I read that the mysqldump command will get the job done but I cant manage to get the correct syntax. Can someone post an example code for mysqldump command? Thank you.
$command="mysqldump --xml ";
Try the script on this page: http://www.chriswashington.net/tutorials/export-mysql-database-table-data-to-xml-file
By any chance you are not trying to run that command inside mysql_query are you ? It wont work that way. mysqldump is a command line utility.
To run it from php you would need to use the system() function, documentation - http://php.net/manual/en/function.system.php
If you are on a shared host with PHP in safe mode, or the system function is explicitly disabled in php.ini, then you will not be able to do this.
In that case you would need to read the data from your table using a SELECT query and iterating on all rows and potting it into an XML file, using XMLWriter or DOMDocument

Inserting a phpmyadmin sql dump without phpymadmin

I'm moving a site that had access to phpmyadmin to one where I don't (not yet anyway). Is there a php script to import the generated .sql file into a database? The db is created and ready, just need to import the tables and records.
Try this.
Upload your SQL file to the web space via FTP and execute a page with this code in it.
<?php
$file="path/to/file.sql";
$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command);
?>
Don't forget to set the variables for database name, username, and password. Also, make sure PHP has access to execute commands using the exec function.
Why use php, why not use MySQL itself:
http://dev.mysql.com/doc/refman/5.5/en/mysql.html
Use SSH. Install PuTTy first. Ask your host the server IP, username and password for SSH server, and then do the work. Anyway, how do you think you are going to properly manage your tables and databases without phpmyadmin or any other alternative SQL client, eh? Ask your hosts to install them. Btw, looks like your host's at Antarctica or some other ancient place. I mean, come on man, a SQL client like phpMyAdmin is offered even in free subhosting.

Categories