2

I tried to backup my database following the instruction from SQL Server: Database stuck in "Restoring" state. but i have my database stuck in "Restoring" state. php code below:

$database = "container";
$uid = "sa";
$pwd = "12345" ; 

try {
      $conn = new PDO( "sqlsrv:Server=localhost\SQLEXPRESS;Database=$database", 
          $uid, 
          $pwd
          //,array(PDO::ATTR_PERSISTENT => true)
          ); 
  $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); }
catch( PDOException $e ) {
  die( "Error connecting to SQL Server" ); }

echo "Connected to SQL Server\n";

$backfile = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\sql server bk' ;
echo "Connected to SQL Server\n";
$backfile = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\sql server bk'  ;

$query = "RESTORE DATABASE child_database FROM DISK = '$backfile' WITH  REPLACE, RECOVERY ";
$conn->exec($query);

After running this code I got my child_database stuck in restoring state.however I copy the sql command

RESTORE DATABASE child_database FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\sql server bk' WITH  REPLACE, RECOVERY

and run in microsoft sql server management tool it works just fine.

I try to run add php code

$conn->exec("RESTORE DATABASE child_database WITH RECOVERY");

I got an exception.

please help.

Community
  • 1
  • 1
channa ly
  • 9,479
  • 14
  • 53
  • 86
  • Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]The database cannot be recovered because the log was not restored.' in C:\xampp\htdocs\mssql\restore.php:31 Stack trace: #0 C:\xampp\htdocs\mssql\restore.php(31): PDO->exec('RESTORE DATABAS...') #1 {main} thrown in C:\xampp\htdocs\mssql\restore.php on line 31 – channa ly Jan 30 '12 at 06:48
  • I haven't written any PHP for a LONG time, can you just stick $backfile in a string like that? – tnktnk Jan 30 '12 at 07:10
  • yes. the code run perfect. It restored the database but left the database in restoring state. – channa ly Jan 30 '12 at 07:24

1 Answers1

3

It seems have problem with PDO driver, this problem may be in the ODBC/SNAC layer on which the driver is built. But this database restoring works well with other drivers. I tried with sqlsrv and mssql, they worked well. For more detail, see Restoring a SQL Server Database from PHP

Sokha RUM
  • 86
  • 5