In my site there have an installation part.In that the database is imported(the datbase backup file is in the root folder).So i want to list the tables name after the database import is done.That means if there have 10 tables , if table1 is imported then list table1 is imported ,if table2 is imported then list table2 is imported.
How can i do this?.
This is my ajax code;
function run_install()
{
//alert("process.php");
$.ajax({
type: "POST",
url: "process.php",
data: {method: 'database'},
success: function(msg){
//alert(msg);
}
});
}
This is the functions in process.php page:
if($method == "database") {
$base_url=$_SESSION['base_url'];
$db_name=$_SESSION['db_name'];
$uname=$_SESSION['uname'];
$db_pwd=$_SESSION['pwd'];
$db_host=$_SESSION['db_host'];
clear_db($db_host,$uname,$db_pwd,$db_name);
$conct=mysql_connect($db_host,$uname,$db_pwd);
mysql_select_db($db_name,$conct);
if(mysql_install_db($db_name, "launchrock.sql", $errmsg))
{
}
else{
echo 0;
}
}
function mysql_install_db($dbname, $dbsqlfile, &$errmsg)
{
$result = true;
if(!mysql_select_db($dbname))
{
$result = mysql_query("CREATE DATABASE $dbname");
if(!$result)
{
$errmsg = "could not create [$dbname] db in mysql";
return false;
}
$result = mysql_select_db($dbname);
if(!$result)
{
$errmsg = "could not select [$dbname] database in mysql";
return false;
}
}
else{
$result = mysql_import_file($dbsqlfile, $errmsg);
//$result = "dfgdfG";
return $result;
}
}
function mysql_import_file($filename, &$errmsg)
{
/* Read the file */
$lines = file($filename);
if(!$lines)
{
$errmsg = "cannot open file $filename";
return false;
}
$scriptfile = false;
/* Get rid of the comments and form one jumbo line */
foreach($lines as $line)
{
$line = trim($line);
if(!@ereg('^--', $line))
{
$scriptfile.=" ".$line;
}
}
if(!$scriptfile)
{
$errmsg = "no text found in $filename";
return false;
// $status==0;
}
/* Split the jumbo line into smaller lines */
$queries = explode(';', $scriptfile);
/* Run each line as a query */
foreach($queries as $query)
{
$query = trim($query);
if($query == "") { continue; }
if(!mysql_query($query.';'))
{
$errmsg = "query ".$query." failed";
return false;
// $status==0;
}
else
{
$status=1;
}
}
if($status==1)
{
return true;
}
}
You can get a list of tables in a schema by using the SHOW TABLES sql statement.
Are you aware that you can automatically run a bunch of SQL statements by using the mysql command-line client? Assuming your backup is an sql dump of the schema and data you could just run
Depending on your server/PHP set up you may be able to run this using the shell_exec() function. Note that this may mean your username and password are visible to other users on the same server via ps but you can get round this by using an options file.