Solved

php script to convert mysql table

Posted on 2002-04-11
10
252 Views
Last Modified: 2008-03-10
Hi,

I need to take the data out of my table 'links' (structure below) and transfer it into my table 'nuke_links_links' (structure also below) which is contained in a different database, but on the same server. I figured that the best way to do this would be with a php script, but I don't know enough about php to write one... I actually tried to do this just using phpmyadmin but was pretty unsucessful...

any ideas anyone? I've put the max number of question points up for this...

# Table structure for table `links`
#

CREATE TABLE links (
  ID int(11) NOT NULL auto_increment,
  SiteName varchar(100) NOT NULL default '',
  SiteURL varchar(100) NOT NULL default '',
  LastUpdate timestamp(14) NOT NULL,
  Added timestamp(14) NOT NULL,
  Description blob NOT NULL,
  Category smallint(6) unsigned NOT NULL default '0',
  UserName varchar(16) NOT NULL default '',
  Password varchar(16) NOT NULL default '',
  Hint varchar(50) NOT NULL default '',
  Email varchar(50) NOT NULL default '',
  InIP varchar(15) NOT NULL default '',
  OutIP varchar(15) NOT NULL default '',
  HitsIn int(7) unsigned NOT NULL default '0',
  HitsOut int(7) unsigned NOT NULL default '0',
  PRIMARY KEY  (ID),
  UNIQUE KEY SiteURL (SiteURL),
  KEY Category (Category),
  KEY HitsIn (HitsIn),
  KEY HitsOut (HitsOut)
) TYPE=MyISAM;
# --------------------------------------------------------

importing into this:

# Table structure for table `nuke_links_links`
#

CREATE TABLE nuke_links_links (
  pn_lid int(11) NOT NULL auto_increment,
  pn_cat_id int(11) NOT NULL default '0',
  pn_title varchar(100) NOT NULL default '',
  pn_url varchar(100) NOT NULL default '',
  pn_description text NOT NULL,
  pn_date datetime default NULL,
  pn_name varchar(100) NOT NULL default '',
  pn_email varchar(100) NOT NULL default '',
  pn_hits int(11) NOT NULL default '0',
  pn_submitter varchar(60) NOT NULL default '',
  pn_ratingsummary double(6,4) NOT NULL default '0.0000',
  pn_totalvotes int(11) NOT NULL default '0',
  pn_totalcomments int(11) NOT NULL default '0',
  PRIMARY KEY  (pn_lid)
) TYPE=MyISAM;

0
Comment
Question by:1524
  • 5
  • 3
  • 2
10 Comments
 
LVL 4

Expert Comment

by:lokeshv
Comment Utility
just a small ex for u ...


modify that according to ur requirement

table1

table1_col_1
table1_col_2
table1_col_3


table2

table2_col_1
table2_col_2

suppose now u have to grab the data(col2 and col3) and dump into the table2


so

query1="select database1.table1.table1_col_2,database1.table1.table1_col_3 from database1.table1 ";

$qid=mysql_db_query($query1);

if($qid){
        for($i=0;$i<mysql_numrows($qid);$i++){
                $row=db_fetch_array($qid);
                 add_data_in_table2($row[table1_col_2],$row[table1_col_3]);
         }
}
else{
        echo'Errror:'.mysql_error();

}



function add_data_in_table2($col1,$col2){
$query2="insert into database2.table2(database2.table2.table2_col_1,database2.table2_col_2) values('$col1','$col2')";

$qid=mysql_db_query($query2);

if($qid){
     return 1;
}
else{
        echo'Error:'.mysql_error();
}
}



Hope this will help...

Lk
0
 

Author Comment

by:1524
Comment Utility
Hi, Thanks for your help, I just tried using your example but I must still have something wrong... here's the error I got:

Warning: Wrong parameter count for mysql_db_query() in /home/sites/site61/web/convert.php on line 4
Errror:

and here's the code I used:

<?php
$query1="select underground.links.links_col_2,underground.links.links_col_3 from underground.links";

$qid=mysql_db_query($query1);

if($qid){
       for($i=0;$i<mysql_numrows($qid);$i++){
               $row=db_fetch_array($qid);
                add_data_in_nuke_links_links($row[links_col_2],$row[links_col_3]);
        }
}
else{
       echo'Errror:'.mysql_error();

}



function add_data_in_nuke_links_links($col1,$col2){
$query2="insert into main.nuke_links_links(main.nuke_links_links.nuke_links_links_col_1,main.nuke_links_links_col_2) values('$col1','$col2')";

$qid=mysql_db_query($query2);

if($qid){
    return 1;
}
else{
       echo'Error:'.mysql_error();
}
}
?>

Thanks in advance!

--

Ben
0
 
LVL 4

Expert Comment

by:lokeshv
Comment Utility
ok do some changes first make a database connection

on top of script..

$conn=mysql_pconnect("host","user","password");

now replace all mysql_db_query with

mysql_db_query("database_name",$query) use query1 and query2 respectively...

and i used the col_1 and col_2 just and ex.

u have to relace those from ur tbale col names..

i m goin out this time..when i will return i will post the full code here...



Lk
0
 
LVL 4

Expert Comment

by:lokeshv
Comment Utility
cna u please post here whihc columns u want o grab and wannt to put in whihc column ?


Lk
0
 
LVL 5

Expert Comment

by:harwantgrewal
Comment Utility
I think by using the phpmyadmin is will be preety simple first generate a txt file and then use this text file to import the data into any table into any database but first of all check that structure of both the table is same

Harry
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:1524
Comment Utility
Hi lokeshv,

here's how I would ideally like to transfer the data across:

thanx in advance! :)

--

Ben

ID
SiteName          pn_title
SiteURL           pn_url
LastUpdate
Added,            pn_date
Description       pn_description
Category          pn_cat_id
UserName          pn_submitter
Password
Hint
Email             pn_email
InIP
OutIP
HitsIn
HitsOut           pn_hits
0
 
LVL 5

Expert Comment

by:harwantgrewal
Comment Utility
have to try to output txt file from phpmyadmin I think it will be easier to you

Harry
0
 
LVL 4

Accepted Solution

by:
lokeshv earned 300 total points
Comment Utility
here it is ..

just add ur database server setting and the database_name1
and database_name2...


<?
$conn=mysql_pconnect("database_server","user","password");

if(!$conn){
        echo'couldnt connect';
        die;
}



$query="insert into database_name2.nuke_links_links(pn_title,pn_url,pn_date,pn_description,pn_cat_id,pn_submitt
er,pn_email,pn_hits) select SiteName,SiteURL,Added,Description,Category,UserName,Email,HitsOut from database_na
me1.links";

$qid=mysql_query($query);
if($qid){
        echo'table updated';
}
else{
        echo'error:'.mysql_error();
}
?>



Hope this will solve problem...

Lk:o)
0
 

Author Comment

by:1524
Comment Utility
Thanks!!!! youre a lifesaver! much appreciated :)
0
 
LVL 4

Expert Comment

by:lokeshv
Comment Utility
anytime :o)


LK
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now