SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Copying Tables with php?
-
May 10, 2001, 07:45 #1
I really want to make a copy of one of my tables. I dont suppose it's relivent, but I'll explain why anyway. My db design wasn't so good when I made my original site, I've now thought of better ways to do stuff, so I'd like to change the tables's, however I dont want to take my site down, I'd rather let people surf it and work on the new system behind the scenes, I can't do that if my changes to the articles database are affecting the site. If I had a copy I could fix it all up then just upload the new scripts.
Basically, I'd like to copy all the contents of the articles table into say a new table 'articles2' ... I dont have comand line access. Is there a way to do this? I'd reallly appreciate it.
-
May 10, 2001, 10:23 #2
- Join Date
- Feb 2001
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is my run at it.
PHP Code://database connection info here
$sql = "Select * from Table";
$res = mysql_query($sql);
while ($row = msyql_fetch_array($res)) {
$sql = "Insert into Table2 (field1, field2, etc) values ('$row[field1]', '$row[field2]', etc)";
$res = mysql_query($sql);
}
Marty H.
-
May 10, 2001, 12:09 #3
Thanks, I'll use that method, if anybody knows of a built in mysql function to copy a table, however, that would be ideal.
Thanks again, Marty.
-
May 11, 2001, 00:05 #4
- Join Date
- Feb 2001
- Posts
- 21
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you're using MySQL 3.23, check out the new CREATE TABLE option:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement]
...notice the select_statement option at the end. I haven't used this personally, as I just use mysqldump from the command line. It should work for your purposes, though. Use create table to create a new table with the same structure as the old, and add a "select * from articles" to the end of the create table statement.
Good luck!
Bookmarks