Loading CSV file into MYSQL database not importing rows - php

I have the following function:
function bulk_insert_file($filename) {
$file_location = 'assets/temp/'.$filename;
$sql = 'LOAD DATA LOCAL INFILE '."'$file_location'".' INTO TABLE p4p.users_csv_import
FIELDS TERMINATED BY \',\'
LINES TERMINATED BY \'\\r\\n\'
IGNORE 1 LINES';
$this->db->query($sql);
}
And the following CSV file:
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
However, when I run the code no data is imported. If I remove the IGNORE 1 LINES part I will at least get the first row imported.
CREATE TABLE `users_csv_import` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL DEFAULT '1',
`unique_id` varchar(255) DEFAULT '',
`first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`last_name` varchar(50) CHARACTER SET latin1 NOT NULL,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`company` int(11) NOT NULL DEFAULT '0',
`nonprofit` int(11) NOT NULL,
`username` varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT '',
`password` varchar(34) CHARACTER SET latin1 NOT NULL DEFAULT '',
`dimension_data` text,
`raw_csv_data` text,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here is the specs on the table..

I assume the csv file is in unix line terminators format.
You should therefore try importing with LINES TERMINATED BY \'\\n\'.

Related

LOAD data infile not importing datetime

I have csv file which looks:
0,00018332,2016-08-29 00:00:00,2016-08-29 00:00:00,9999,Sale of Parts only,0,DMS Maritime Pty Ltd,xyz,,00000250,1971-01-01 00:00:00,1971-01-01 00:00:00,218335,Sale of parts only,9999
Then I have php script which is importing this csv into table
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "test.csv";
$dbtable = "res";
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$dbtable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator));
But when I check it back in db table, values 2016-08-29 00:00:00 (column is datetime) is 0000-00-00 00:00:00 , 218335 and 9999 is set to 0 event columns are set to int(11).Everything else is written correctly. I tried with csv and txt and I have same problem.
Do you know why?
EDIT:
CREATE TABLE `res` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
`start` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`room_id` int(11) DEFAULT NULL,
`status` varchar(30) DEFAULT NULL,
`paid` int(11) DEFAULT NULL,
`customer` varchar(50) DEFAULT NULL,
`name_ship` varchar(50) DEFAULT NULL,
`equipment` text,
`port` varchar(30) DEFAULT NULL,
`ETA` datetime DEFAULT NULL,
`ETD` datetime DEFAULT NULL,
`service_id` int(11) NOT NULL,
`service_classification` varchar(30) DEFAULT NULL,
`partners_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5392 DEFAULT CHARSET=latin1
Your file is encoded in utf16, mysql will not check that on its own. Either change that in your datasource (since your table encoding indicates that you don't want to use utf16 strings anyway) or inform mysql about the file's encoding.
For your file, the following should work:
LOAD DATA LOCAL INFILE 'test.csv' INTO TABLE res
CHARACTER SET utf16le
FIELDS TERMINATED BY ';\0'
LINES TERMINATED BY '\r\0\n\0';
It sets the characterset of the file and defines the delimiters in unicode. You can use just '\n\0' too.
Why not execute : show warnings in mysql cmd and see what's the exact error.
https://dev.mysql.com/doc/refman/5.5/en/show-warnings.html

mysql can't insert in a table with 8 columns

i'm trying to insert datas in a table of 8 columns but it's doesn't work . i'm using mysql version 5.7.11 . however it works when i reduce it to 6 columns 7 columns refuse to work as well .
please find the tables and their php code below
6 columns mysql/php
1.1 php
$res=$pdo->prepare('INSERT INTO '.$table.'(title,description,image,city,price) VALUES(:titl,:decri,:img,:cty,:prce)');
$res->bindParam(':titl',$title);
$res->bindParam(':decri',$decri);
$res->bindParam(':img',$item_img);
$res->bindParam(':cty',$city);
$res->bindParam(':prce',$price);
1.2 mysql
CREATE TABLE `mobiles`
(`id` int(11) NOT NULL,
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`image` longblob NOT NULL,
`city` varchar(20) NOT NULL,
`price` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
8 columns tables
2.1 mysql code
CREATE TABLE `mobiles` (
`id` int(11) NOT NULL,
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`image` longblob NOT NULL,
`city` varchar(20) NOT NULL,
`price` int(11) NOT NULL,
`user_id` int(10) NOT NULL,
`date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
2.2 php code
$res=$pdo->prepare('INSERT INTO'.$table.'(title,description,image,city,price,user_id,date) VALUES(:titl,:decri,:img,:cty,:prce,:user_id,:dte)');
$res->bindParam(':titl',$title);
$res->bindParam(':decri',$decri);
$res->bindParam(':img',$item_img);
$res->bindParam(':cty',$city);
$res->bindParam(':prce',$price);
$res->bindParam(':user_id',$user_id);
$res->bindParam(':dte',$date);
i have executed right after , the six columns works but the 8 columns one
why is that more than 6 columns mysql can't insert while the number limit of columns can reach 255.
is there any issue in my code ? i echoed all the entries and they all work fine.
any help please ?
You have no space between 'INSERT INTO' and $table in second query, so query fails because of syntax error.

Set names and collation UTF8 does not works in SimpleXML or MySQL

I am gettin XML data which header has utf8 setted:
<?xml version="1.0" encoding="utf-8"?>
I download it using CURL and it is downloaded correctly with UTF8 chars.
I parse that downloaded xml files using SimpleXML using this code:
<?php
class XMLParser{
public function __construct(){
}
public function parseFile($path){
$xml = '';
if(!is_null($path)){
$file = file_get_contents($path);
if(!is_null($file)){
$xml = new SimpleXMLElement($file);
}
else{
echo 'there is no file';
}
}
else{
echo 'Wrong path!';
}
return $xml;
}
}
?>
And send it to database calling PDO and stored procedures, connect script has defined utf8:
$this->PDO->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
Each table has utf8_general_ci
But in table are chars like this:
León de Huánuco - Cienciano
UPDATE!
CREATE TABLE code:
CREATE TABLE IF NOT EXISTS `jcjdkdtaur`.`fixtures` (
`Id` INT(32) NOT NULL,
`UpdatedDate` varchar(100) DEFAULT NULL,
`DateTime` datetime NULL DEFAULT NULL,
`Date` date NULL DEFAULT NULL,
`Time` time NULL DEFAULT NULL,
`Status` VARCHAR(40) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`League` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`LeagueID` INT(11) DEFAULT NULL,
`SportID` tinyint DEFAULT 1,
`SportName` varchar(30) DEFAULT 'football',
`Cup` varchar(20) DEFAULT NULL,
`Country` VARCHAR(60) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`EventName` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`HomeTeam` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`HomeTeam_Id` INT(11) NULL DEFAULT NULL,
`AwayTeam` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NULL DEFAULT NULL,
`AwayTeam_Id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`Id`,`HomeTeam_Id`,`AwayTeam_Id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
INSERT STORE PROCEDURE I CALL IT FROM PHP:
DROP PROCEDURE IF EXISTS insupd_Fixtures;
DELIMITER //
CREATE PROCEDURE insupd_Fixtures(IN p_id int(32), IN p_updated varchar(100), IN p_datetime datetime, IN p_date date,
IN p_time time, IN p_status varchar(40),IN p_league varchar(255),IN p_leagueid int(11), IN p_sportid tinyint, IN p_sportname varchar(30),
IN p_cup VARCHAR(20), IN p_country VARCHAR(60),IN p_eventname varchar(255),IN p_hometeam varchar(255),IN p_hometeam_id int(11),
IN p_awayteam varchar(255),IN p_awayteam_id int(11)
)
BEGIN
INSERT INTO fixtures(`Id`,`UpdatedDate`,`DateTime`,`Date`,`Time`,`Status`,`League`,`LeagueID`,`SportID`,`SportName`,`Cup`,`Country`,`EventName`,
`HomeTeam`,`HomeTeam_Id`,`AwayTeam`,`AwayTeam_Id`)
VALUES
(p_id,p_updated, p_datetime, p_date, p_time, p_status,p_league, p_leagueid, p_sportid, p_sportname, p_cup,p_country,
p_eventname,p_hometeam,p_hometeam_id,p_awayteam,p_awayteam_id)
ON DUPLICATE KEY UPDATE
Id=p_id,
UpdatedDate = p_updated,
DateTime=p_datetime,
Date=p_date,
Time=p_time,
Status=p_status,
League=p_league,
LeagueID = p_leagueid,
SportID = p_sportid,
SportName = p_sportname,
Cup=p_cup,
Country=p_country,
EventName=p_eventname,
HomeTeam=p_hometeam,
HomeTeam_Id=p_hometeam_id,
AwayTeam=p_awayteam,
AwayTeam_Id=p_awayteam_id;
END //

Am I getting an SQL syntax error

CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT,
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
INSERT INTO `contracts` (`contractId`, `username`, `title`, `contractText`, `date`, `contractState`) VALUES
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(32) CHARACTER SET utf8 NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
`email` varchar(20) CHARACTER SET utf8 NOT NULL,
`usergroup` tinyint(1) NOT NULL COMMENT,
`name` varchar(32) CHARACTER SET utf8 NOT NULL,
`lastname` varchar(32) CHARACTER SET utf8 NOT NULL,
`state` tinyint(4) NOT NULL COMMENT,
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
When running this query on the SQL server, I am getting the following error:
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
title varchar(20) CHARACTER SET utf8 NOT NULL,
contractText text CHAR' at line 13
why this is coming up?
You did not add a comment to the username column.
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT ,
add one here---------------------------------------^
or remove the COMMENT keyword
in this line
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
add some comment in for COMMENT or Remove Comment. Below is working code.
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3-
panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Just fix your table creation to add explicit comment or remove COMMENT keyword if it is not needed.
Try modify it to become:
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'add explicit comment her',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
Remove comment from this line:
`username` varchar(32) CHARACTER SET utf8 NOT NULL , //COMMENT

PHP MySQL CREATE TABLE

I'm trying to create a table using PHP, PDO and MySQL.
For the needs of my application, the name of the table has to be a variable.
Here is my code :
$request = $pdo->prepare("CREATE TABLE IF NOT EXISTS :table (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;");
$request->execute(array(
'table'=>$uuid));
Can't I use ":table" in the MySQL statement ??
Currently I wrote :
[...]
CREATE TABLE IF NOT EXISTS `$uuid`
[...]
This works but it sounds weird to me ^^' Is it the only solution to my problem ?
You can't pass the table name as parameter. If you want to create table with variable name you must use dynamic query.
$pdo->query("CREATE TABLE IF NOT EXISTS userfiles (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned NOT NULL,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
This is THE ONLY proper way of handling such situations.
Such matters are very basic things.
and your current setup is just like a car with square wheels.
Despite of your shortage of time you have to make it single table.
Otherwise you will waste A LOT more time and eventually will turn to the proper design anyway but after innumerable pains

Categories