Jump to content

importing a .CSV to mysql


joshuaceo

Recommended Posts

How can I import a .csv file to a mysql database?

 

Or at least a long list.

 

For example i would like to import this list into a mysql database:

 

assets/images/economyhalt12401_thumbnail.jpg

assets/images/economyhalt12401_thumbnail.jpg

assets/images/165310-bag-in-a-bag_thumbnail.jpg

assets/images/123810_thumbnail.jpg

assets/images/polos1_thumbnail.gif

assets/images/stlourdnsheepboots_thumbnail.jpg

assets/images/double-safety.05_thumbnail.jpg

assets/images/148684_thumbnail.gif

assets/images/203111_thumbnail.gif

assets/images/106242_thumbnail.gif

assets/images/203110_thumbnail.gif

assets/images/203109_thumbnail.gif

assets/images/stud-plugs_thumbnail.gif

assets/images/stud-plugs(1)_thumbnail.gif

assets/images/ez-mount-western_thumbnail.gif

assets/images/239407_thumbnail.jpg

assets/images/239411_thumbnail.jpg

assets/images/239415-braided-visalia_thumbnail.gif

assets/images/239419d-dark-stained_thumbnail.gif

assets/images/246033l-stirrup_thumbnail.jpg

assets/images/239408_thumbnail.jpg

assets/images/239412_thumbnail.jpg

assets/images/shenshowreins_thumbnail.jpg

assets/images/159012(1)_thumbnail.gif

assets/images/shendouble-stitchreins(1)_thumbnail.jpg

assets/images/159013_thumbnail.gif

assets/images/159010_thumbnail.gif

assets/images/nw50_thumbnail.gif

assets/images/159014_thumbnail.gif

assets/images/beta-combo-bridle_thumbnail.jpg

assets/images/159016beta-rein_thumbnail.jpg

assets/images/race-bridle_thumbnail.gif

 

*it would be much longer, this is just a sample

Link to comment
https://forums.phpfreaks.com/topic/135768-importing-a-csv-to-mysql/
Share on other sites

This code will help you:-

 

first upload the csv file at a location into csv folder

 

$uploaddir = 'csv/';

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

$find=".csv";

$str=$_FILES['file']['name'];

$pos=strpos($str, $find);

	if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
		$msg="File is valid, and was successfully uploaded.";

	} 
	else 
	{
		$msg="Possible file upload attack!";
	}

//file has been uploaded now import the file contents to the db

$handle = fopen($uploadfile, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }

//the csv file contains two data i.e. $data[0] contains name field and $data[1] contains email field
$name=$data[0];
$emailID=$data[1];

		$query=mysql_query("SELECT * FROM details where emailID='$emailID'");

		$n=mysql_num_rows($query);

		if($n==0) 
		{

			$query=mysql_query("INSERT INTO `details`(`name`, `emailID`) VALUES('$name', '$emailID')");

			$ins++;
		}
}
fclose($handle);

?>

 

cheer! enjoy

 

check this website http://www.mysqlandphp.net

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.