Import MySql From CSV

Continuing from my previous article about Export MySql Into CSV, I will explain about how to Import MySql From CSV. In this case, you will need to load the CSV file by using a php file that placed in the destination server. The php file will read the CSV file and then make a MySql query related to the content of the CSV file.

Here the php file:

$host = 'localhost';//host name, in this case we use localhost
$user = 'root';//user name, in this case we use root
$pass = 'password';//password, in this case we use password
$db = 'member';//declare the database that will be used

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$handle = @fopen("csv.csv", "r");
$content = '';
if ($handle) {
    while (($buffer = fgets($handle)) !== false) {
        $content .= $buffer;
    }
}
$arr1 = explode("\n",$content);
$table = '';
foreach($arr1 as $val){
	preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"/i',$val, $matches);
	$q = "INSERT INTO `member` (`member_id`, `name`, `phone`, `address`, `status`) VALUES ('".$matches[1][0]."', '".$matches[1][1]."', '".$matches[1][2]."', '".$matches[1][3]."');";//you can change this based on the table that will you insert.
	mysql_query($q);
}
fclose($handle) or die("can't close file");

You can change the $q based on the table that you want to inser. In this case, the code above will only export-import one table. However, with little bit modification you can make your own code for export-import multiple table. Good luck.

http://septiadi.com/2011/05/30/import-mysql-from-csv/

Advertisements

Export MySql Into CSV

After examine the different between XML and CSV file, I found out that when you export a database to XML and CSV files, the CSV file should be smaller than the XML file. Therefore, in this article I will explain about how to Export MySql Into CSV.

This article is a little bit simillar with my previous post about Export MySql Into XML. The different is we will use CSV this time.

$host = 'localhost';//host name, in this case we use localhost
$user = 'root';//user name, in this case we use root
$pass = 'password';//password, in this case we use password
$db = 'member';//declare the database that will be used
$table = 'tos';//table that will be exported
$file = 'export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_array($values)) {
	foreach($rowr as $key => $val){
		if (!preg_match('#^\d+$#', $key, $match)){
			$csv_output .= '"'.$val.'";';
		}
	}
	$csv_output = rtrim($csv_output, ";");
	$csv_output .= "\n";
}

$filename = $file."-".$db."-".$table."_".date("Y-m-d_H:i:s",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
echo $csv_output;
exit;

When the file is executed, the file will generate an CSV file. I will post how to import database from an CSV file next time. Hope this simple article can help you alot.

http://septiadi.com/2011/05/30/export-mysql-into-csv/


Export MySql Into XML

Previously I just want to export several tables of my database from one server to another server. Let say, I want to send tables from server A to server B. In this case, I just make a php file that will generate xml file when executed. In this case, the php file to Export MySql Into XML will be placed in server A.

Here the codes:

//connecting to DB
$host = 'localhost';//host name, in this case we use localhost
$user = 'root';//user name, in this case we use root
$pass = 'password';//password, in this case we use password
$conn = mysql_connect($host, $user, $pass)
or die('Connection Failed');//will show "Connection Failed" if the connection failed
$db_name = 'member';//declare the database that will be used
mysql_select_db($db_name);//selecting the data base

//tables that will be exported
$tables = array("client","ib");

header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="'.$table.'.xml"');

$text ='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<'.$db_name.'>';

foreach($tables as $table){
	$result = mysql_query("SELECT * FROM  `$table`");
	$text .='
	<table_'.$table.'>';

	while($row=mysql_fetch_array($result)) {
		$text .=  "
		<$table>";
			foreach($row as $key => $val){
				if (!preg_match('#^\d+$#', $key, $match)){
					$text .=  "
					<$key>$val</$key>";
				}
			}
		$text .=  "
		</$table>";
	}

	$text .= "
	</table_$table>";
}
	$text .= "
</$db_name>";
echo $text;
mysql_close($connection);

When the file is executed, the file will generate an xml file. I will post how to import database from an xml file next time. Hope this simple post can help you alot.

http://septiadi.com/2011/05/20/export-mysql-into-xml/


PHP Session Expire

PHP Session Expire

Previously, I just search through the internet to find PHP Session Expire. However, I couldn’t find what I need.

In this case, I just want to make my system will expire the session if the user dont have activity on the page. But, I havent found php function to check the status of user, because PHP is server side. Despite of checking the user activity on the page, I rather choose to expire the session if the user not refreshing the page for some time.

Here is some code to expire the session if the user dont refresh the page.

session_start();
$timeout = 300;//in second
if(isset($_SESSION['time_stamp']) ) {
	$idle_time = time() - $_SESSION['time_stamp'];
	if($idle_time > $timeout){
		session_destroy();
		echo "Session Destroyed";
		//header('Location:logout.php');
		//some code when session is expired
	}
	else{
		$_SESSION['time_stamp'] = time();
		//refreshing time stamp
	}
}
else{
	$_SESSION['time_stamp'] = time();
	//make time stamp
}

Just to keep in mind PHP Session Expire script have to be placed on the top of every page. Hope this PHP Session Expire can help you alot.

 

http://septiadi.com/2011/05/19/php-session-expire/


Facebook Like Button

Facebook Like Button

Facebook Like Button is The Like button provided by Facebook to lets users share pages from site back to their Facebook profile with one click. To implement Facebook Like Button is quite simple. What you need is to copy and paste some code that you can generate it from http://developers.facebook.com/docs/reference/plugins/like/.

This is some example code:

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:like href="[URL to be liked]" width="150" show_faces="false" font=""></fb:like>

What you have to keep in mind is the [URL to be liked]. You can change [URL to be liked] into any url. In this case, the url of “http://yourdomain.com/post.php?post=1&#8221; will be different with “http://yourdomain.com/post.php?post=2&#8221;. Therefore, each page in your domain may have exactly one particular Facebook Like Button that different with Facebook Like Button from other page.

http://septiadi.com/2011/05/12/facebook-like-button/


Facebook Like Button Facebook Like Button Facebook Like Button Facebook Like Button Facebook Like Button Facebook Like Button


Facebook Logout

Facebook Logout

Facebook Logout is a function used to make the uer logout after login into your application using facebook login. In this case, what you need is quite simple. What you need just make a button that will trigger a facebook logout function.

Here is the logout function :

function facebook_logout(){
	FB.logout(function(response) {
		//code when user successfuly logged out
	});
}

In this case, when user push a button that will trigger the Facebook Logout, the fb:login-button tag will automatically changed.

http://septiadi.com/2011/05/12/facebook-logout/


Facebook Logout Facebook Logout Facebook Logout Facebook Logout Facebook Logout Facebook Logout