Restore a MySql table using a text file.

By using next code we can restore a MySql table from a text file which we have data of table. This file is have backup data from table using PHP earlier.

Steps to Upload file in table.



define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PW', 'uyw487hks');
define('DB_DB', 'databaseName');






require_once('dbinfo.inc'); 
$conn=mysql_connect (DB_SERVER,DB_USERNAME,DB_PW) or die('Cannot 
connect to the database because: ' . mysql_error());
mysql_select_db (DB_DB);






 global $obj_mysql;
 $string="";
$csv_output ="";
 

 $table="busi_data";
$i = 0; 

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$output .= $row['Field']."; ";
$i++;
}
}



//echo $output;



//mysql_query("DELETE FROM comp_data ");

$string="";

 $filename = "textFile.txt";
 $i=0;
 
   $fh1 = fopen("$filename",'r');
   while(!feof($fh1))
    {
   $char1 = fgetc($fh1);
   
   if($char1!=";"){
   $string .=$char1;
   
   }
   else{
   echo "$string;";
 //  echo ";";
 //  echo "i Value $i";
 //  echo $string;
 //  id; compName; mobNo; mobNo1; mobNo2; phNo; fax; email; city;
 compAdd; state; country; ownerName; busiCat; serial; prior; show_lev; upload_date; user;
 //id; compName; title; tags; mobNo; mobNo1; mobNo2; phNo; fax; email; city; compAdd; state; country; ownerName; busiCat; serial; prior; show_lev; upload_date; user; 
    if($i==1){$comp_name=$string;}
	if($i==2){$title=$string;}
	if($i==3){$tags=$string;}
    if($i==4){$mobNo=$string;}
	if($i==5){$mobNo1=$string;}
	if($i==6){$mobNo2=$string;}
	if($i==7){$phNo=$string;}
	if($i==8){$fax=$string;}
	if($i==9){$email=$string;}
	if($i==10){$city=$string;}
	if($i==11){$compAdd=$string;}
	if($i==12){$state=$string;}
	if($i==13){$country=$string;}
	if($i==14){$ownerName=$string;}
	if($i==15){$busiCat=$string;}
	if($i==16){$serial=$string;}
	if($i==17){$prior=$string;}
	if($i==18){$show_lev=$string;}
	if($i==19){$upload_date=$string;}
	if($i==20){$user=$string;}
	
 
  echo"
$i
"; $i++; $string=""; } if($i==21){ $sql="INSERT INTO busi_data (compName,title, tags, mobNo, mobNo1, mobNo2, phNo, fax, email, city, compAdd, state, country, ownerName, busiCat, serial, prior, show_lev, upload_date, user) VALUES ('$comp_name','$title', '$tags','$mobNo','$mobNo1','$mobNo2','$phNo','$fax','$email','$city','$compAdd','$state','$country','$ownerName','$busiCat','$serial','$prior','$show_lev','$upload_date','$user')"; //$sql="INSERT INTO busi_data (compName) //VALUES ('$comp_name')"; if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error()); $error="Duplicate data same name cannot Uploaded"; } $i=0; } } fclose($fh1);
Valid CSS!

Online Business Solutions

Share |