Hi.
I am trying to create a php script which will drop an existing database, re-create it, and restore the data from an sql file. So far, I have been able to create one which drops and recreates, but I can't figure out how to execute the sql file from php.
I need this script so I can regularly and automatically refresh a demo site which uses mySQL. I have access to creating a cronjob via cPanel, but cannot access the mysql executable so I'm trying to do this via php.
The code I have is:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$query = 'DROP DATABASE demosite';
$result = mysql_query($query);
$query = "CREATE DATABASE demosite";
$result = mysql_query($query);
ob_start();
include('demosite.sql');
$output = ob_get_contents();
ob_end_clean();
mysql_close($conn);
?>
I pieced this together from a php tutorial and this post. The script successfully drops and recreates the database, but does not seem to do anything with the sql file.
What am I doing wrong?
Thanks