2

I wanted to make a simple website, which can open, show, and run sql server queries, just like executing queries from sql server management studio. It already can execute some queries like select (then show it as a table), insert, update, delete, including when i use where/group/order statement. But, when I doing backup on my database with query :

BACKUP DATABASE 'mydb' TO DISK='pathname\filename.bak' WITH INIT/NOINIT

It can only backup the database once, and then when I execute the query with the same inputs, it got out with no error, but also no file backups produced from the execution. I don't know why, but when i'm executing with the same query from sql server management studio (with different filename so that the previous file didn't get overwritten), it execute without error and creating another file backup.

The code : php

$sql = "ALTER DATABASE $db_name SET RECOVERY FULL"; 
$stmt = sqlsrv_query($conn, $sql); 
if($stmt === false) 
{ 
die(print_r(sqlsrv_errors())); 
} 
else 
{ 
echo "Recovery model set to FULL"; 
}
if($_POST['type']=="Backup Full")
{
$query="BACKUP DATABASE $db_name TO DISK='$file\\$filename.bak' WITH
NOINIT;";
    echo "<br>".$query."<br>";
    $stat=sqlsrv_query($conn,$query);
    if($stat == false)
    {
        echo "Error to retrieve info, THIS IS THE ERROR : <br />";
        die(print_r(sqlsrv_errors()));
    }
    else
    {
        echo "Success!";
    }
    }
  1. It create a new file .bak filled with my database backup
  2. It returns error inside the 'if'
  3. When i tried to run the code again with the different filename, it didn't create anymore .bak file.
  4. When i restarted my PC, things back to normal (creating another .bak again)
2
  • And you code... ? Commented Jan 25, 2017 at 16:40
  • Show your existing code... Commented Jan 25, 2017 at 17:09

1 Answer 1

3

It seems there's a solution here. Your issue it happened to me also but in my case sometimes the backup was created and sometimes no and I guess that the backup process fails when php executes and it exits before the ending backup process, therefore, it kills the connection in the middle of backup process. I realized that to execute a sql sequence of no-query type, it has to be consumed with the instruction sqlsrv_next_result till there's no more results.

You should use this snippet in order to execute the backup query successfully.

$query="BACKUP DATABASE $db_name TO DISK='$file\\$filename.bak' WITH NOINIT;";

if ( ($stmt = sqlsrv_query($conn, $query)) )
{
    do 
    {
    } while ( sqlsrv_next_result($stmt) ) ;
    sqlsrv_free_stmt($stmt);
}else{
    die(print_r(sqlsrv_errors())); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

And if using pdo, you can do something like while ($stmt->nextRowset()) {}.

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.