home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / uploadodbc.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  4.7 KB  |  166 lines

  1. Upload ODBC table
  2.  
  3. This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. The table must already exist on MySQL It will figure out which fields are defined in the MySQL table and then upload the coresponding field from the ODBC ressource. If you give it no arguments. It will show you a list of tables in the MySQL database. 
  4.  
  5.  
  6.  
  7.  
  8. <?php 
  9. /* 
  10.  * This include file defines the functions html_header and html_footer 
  11.  */ 
  12. require( "design.inc"); 
  13. $mysqldb =  "concord"; 
  14.  
  15. function nonfatal_error($message) 
  16.     printf( "<H1>%s</H1>\n",$message); 
  17.  
  18.  
  19. function upload_records($odbcfd,$myfd,$mysqldb,$table) 
  20. /* 
  21. * Figure out which columns the mysql database has 
  22. */ 
  23.     $fieldlist = mysql_list_fields($mysqldb,$table); 
  24.     $numof  = mysql_num_fields($fieldlist); 
  25.     for($i = 0; $i < $numof; $i++) 
  26.     { 
  27.         $fieldname = mysql_field_name($fieldlist,$i); 
  28.         if($i == 0) 
  29.             $columnlist = $fieldname; 
  30.         else 
  31.             $columnlist = $columnlist .  "," . $fieldname; 
  32.         $fieldtype = mysql_field_type($fieldlist,$i); 
  33.         switch($fieldtype) 
  34.         { 
  35.         case  "string": 
  36.         case  "datetime": 
  37.             $fieldquotes[$fieldname] =  "'"; 
  38.             break; 
  39.         default: 
  40.             $fieldquotes[$fieldname] =  ""; 
  41.             break; 
  42.         } 
  43.     } 
  44. /* 
  45. * Query the ODBC database and loop through the result. 
  46. */ 
  47.     $res = odbc_exec($odbcfd, "SELECT * FROM $table"); 
  48.     if($res == 0) 
  49.     { 
  50.         nonfatal_error( "Couldn't exec SELECT statement on ODBC database"); 
  51.         return(0); 
  52.     } 
  53. /* 
  54. * Now we know we can query the ODBC database we can delete the records from 
  55. the Mysql 
  56. * database 
  57. */ 
  58.     mysql_db_query($mysqldb, "DELETE FROM $table",$myfd); 
  59.     $rowinx = 0; 
  60.     while(odbc_fetch_row($res)) 
  61.     { 
  62. /* 
  63. * Generate the SQL INSERT STATEMENT 
  64. */ 
  65.         for($i = 0; $i < $numof; $i++) 
  66.         { 
  67.             $fieldname = mysql_field_name($fieldlist,$i); 
  68.             if($i == 0) 
  69.                 $vallist = $fieldquotes[$fieldname] . 
  70.                    addslashes(odbc_result($res,$fieldname)) . 
  71.                    $fieldquotes[$fieldname] ; 
  72.             else 
  73.                 $vallist = $vallist .  "," . $fieldquotes[$fieldname] . 
  74.                    addslashes(odbc_result($res,$fieldname)) . 
  75.                    $fieldquotes[$fieldname] ; 
  76.         } 
  77.         $cmd =  "INSERT INTO $table ($columnlist) VALUES ($vallist)"; 
  78.     if($rowinx % 10 == 0) 
  79.         echo  "<br>"; 
  80.     printf( "%06s\n",$rowinx); 
  81.         if(mysql_db_query($mysqldb,$cmd,$myfd) == 0) 
  82.         { 
  83.             nonfatal_error(mysql_error()); 
  84.         } 
  85.         $rowinx++; 
  86.     } 
  87.     return(1); 
  88.  
  89. /*----------------------------------------------------------------- 
  90. * Start of the program 
  91. */ 
  92.  
  93. /* 
  94. * Connect to the MySQL server 
  95. */ 
  96. $myfd = mysql_connect( "atilia", "guest", "xxxx"); 
  97. if($myfd <= 0) 
  98.     nonfatal_error( "Couldn't open MySQL database"); 
  99.     exit($myfd); 
  100. /* 
  101. * Connect to the ODBC ressource. 
  102. * The DSN must be defined in the executing computer's ODBC tables 
  103. */ 
  104. $odbcfd = odbc_connect( "ConcAccess", "x", "e"); 
  105. if($odbcfd <= 0) 
  106.     nonfatal_error( "Couldn't open ODBC database"); 
  107.     exit($odbcfd); 
  108. function list_tables($mysqldb) 
  109.     echo  "<UL>\n"; 
  110.     $tables= mysql_list_tables($mysqldb); 
  111.     for($i = 0; $i < mysql_num_rows($tables);$i++) 
  112.     { 
  113.         printf( "<LI>Upload table <A HREF=\"upload.php3?table=%s\">%s</A>\n", 
  114.             mysql_tablename($tables,$i), 
  115.             mysql_tablename($tables,$i)); 
  116.     } 
  117.     echo  "</UL>\n"; 
  118. switch ($table) 
  119. case  "": 
  120.     html_header( "Update Database on mysql"); 
  121. ?> 
  122. <P>This page allows you to upload tables from the MS-Access database to the 
  123. database 
  124. on the webserver. There <I>might</I> be some uploading errors. This is the 
  125. reason 
  126. you can only upload one table at a time. You should select a table and then 
  127. look at the 
  128. result for errors.</P> 
  129. <?php 
  130.     list_tables($mysqldb); 
  131.     break; 
  132. default: 
  133.     html_header( "Uploading $table"); 
  134.     echo ( "Row ## Row ## Row ## Row ## Row ## Row ## Row ## Row ## Row ## 
  135. Row ##<br>\n"); 
  136.     $tables= mysql_list_tables($mysqldb); 
  137.     $dotable=0; 
  138.     for($i = 0; $i < mysql_num_rows($tables); $i++) 
  139.         if(mysql_tablename($tables,$i) == $table) 
  140.         { 
  141.              $dotable=1; 
  142.         } 
  143.     if($dotable == 1) 
  144.         upload_records($odbcfd,$myfd,$mysqldb,$table); 
  145.     else 
  146.         nonfatal_error( "There was no such table"); 
  147.  
  148.     list_tables($mysqldb); 
  149.     break; 
  150. odbc_close($odbcfd); 
  151. mysql_close($myfd); 
  152. html_footer(); 
  153. ?> 
  154.