Page 1 of 1

Using PHP to Query One DB and Insert Tables into Another

Posted: Wed Oct 04, 2006 3:13 pm
by greyday
I have two databases. I want to know if it's possible to use PHP to query the first Database and return a SQL file of multiple tables saved as a file in a temp folder. In the same script it would then connect to my second Database, drop the tables if they exist and replace them with the SQL file.

Any help or points in the right direction are greatly appreciated!

Posted: Wed Oct 04, 2006 3:16 pm
by Mordred
phpMyAdmin can do that, if it's about a one-time shot

Posted: Wed Oct 04, 2006 3:21 pm
by greyday
Actually, I need to use it as a function. So "When this happens, go run this php page" that does what I mentioned above. Thanks for the reply though. Any other ideas?

Posted: Wed Oct 04, 2006 7:21 pm
by greyday
Upon more research I dsicovered this, but it doesn't appear to be delivering the file to the folder:

Code: Select all

<?php

$host = "sql.mydomain.com";
$user="XXXXX";
$pass="XXXXX";
$base="sql";

$conn = mysql_connect($host,$user,$pass); 
mysql_select_db($base);

$tableName = 'products';
$backupFile = 'backup/products.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

mysql_close($conn);

?>
Can anyone help me with this?

Posted: Thu Oct 05, 2006 2:36 am
by Mordred
Use PHP tags for php code, not Code!

I think you should use an absolute path there, maybe now the file is created relative to your database bin folder or something.
Be sure to read the docs about SELECT ... INTO OUTFILE, there are possible pitfalls in using it. For example the file should not exist.