Forum Moderators: coopster
So I have two different websites that are using some tables that are the same. One gets the tables updated, and other other needs these updated tables to work correctly. Now I'm trying to make a click script that accesses the updated tables, inserts the new rows, and then empties the updatable table again.
I've gotten to a point where I can echo a dump ( almost the same thing phpmyadmin would output in it's export window) on my second website, and run it in that phpmyadmin sql area to make it insert the info, but if I run mysql_query() on that same echo it it says my sql syntax is wrong or something like that. I'm now confused :p
Here's the code I use to dump my table:
$tab_status = mysql_query("SHOW TABLE STATUS");
while($all = mysql_fetch_assoc($tab_status)):
$tbl_stat[$all[Name]] = $all[Auto_increment];
endwhile;
unset($backup);
$tables = mysql_list_tables("myDatabase");
$tabs[0] = "myTable"; //this is the table i'm trying to move
$backup .= "CREATE TABLE IF NOT EXISTS `$tabs[0]` (";
$res = mysql_query("SHOW CREATE TABLE $tabs[0]");
while($all = mysql_fetch_assoc($res)):
$str = str_replace("CREATE TABLE `$tabs[0]` (", "", $all['Create Table']);
$str = str_replace(",", ",", $str);
$str2 = str_replace("`) ) TYPE=MyISAM ", "`)) TYPE=MyISAM ", $str);
$backup .= $str2."; ";
endwhile;
$data = mysql_query("SELECT * FROM $tabs[0]");
while($dt = mysql_fetch_row($data)):
$backup .= "INSERT INTO `$tabs[0]` VALUES('$dt[0]'";
for($i=1; $i<sizeof($dt); $i++):
$backup .= ", '$dt[$i]'";
endfor;
$backup .= ");\n";
endwhile;
$fName = "mysql ".date("d-m-Y H:i").".txt";
echo $backup;
I didn't write this script, I found it and made some changes though. I know what's going on everyone except for the str_replace functions, I'm not sure what's going down there, just that it outputs it correctly.
Any help would be greatly appreciated.
Secondly, why are you doing this in the first place? Why not use one database and one set of tables for both sites. Why are you copying data and moving it around?
CREATE TABLE IF NOT EXISTS `testMove` ( `test1` int(11) NOT NULL, `test2` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `testMove` VALUES('1', 'val1'); INSERT INTO `testMove` VALUES('2', 'val2');
This is what it says when I run mysql_error() on the query that didn't work:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; INSERT INTO `testMove` VALUES('1', 'val1'); INSERT INTO `testMove` VALUES('2',' at line 4
The code it displayed in the error has changed as I tried to fix it, it always seems to be some random part of the query, I can't tell.
mysql_query($query_one);
mysql_query($query_two);
Also note that you don't need the trailing semi-colon on your queries when using this function.
mysql_query("CREATE TABLE IF NOT EXISTS `testMove` ( `test1` int(11) NOT NULL, `test2` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1");
mysql_query("INSERT INTO `testMove` VALUES('1', 'val1')");
mysql_query("INSERT INTO `testMove` VALUES('2', 'val2')");