I am using the below php script and can't figure out why it is not working. I do get the echo that it processed with no errors in terminal, but the table remains empty. Is there something incorrect with the code causing this. I confirmed it is locating the file in the directory as well. Hopefully something simple is missing.
<?php
try {
$inputfile1 = '////MainDirectory/SubDiretory/data.csv';
$table1 = 'database.testTable1';
$time1 = microtime(true);
require_once("configFile.php"); //this changes the below as now this is the reference point
$mysql_host = DB_HOST;
$mysql_database = DB_NAME;
$mysql_username = DB_USER;
$mysql_password = DB_PASS;
$db = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
} catch (Exception $e) {
die("Unable to connect: " . $e->getMessage());
}
try {
// Return errors
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Begin transaction
$db->beginTransaction();
if(file_exists($inputfile1)) {
$Found = 'found';
// Query D2 Load Temp table
$tempA2 = $db->prepare("LOAD DATA INFILE :inputfile1
INTO TABLE $table1
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(column1, column2, column3);");
$tempA2->bindParam(':inputfile1', $inputfile1);
$tempA2 -> execute();
}
} catch (Exception $e) {
// If transaction fail, use checkpoint and rollback
$db->rollBack();
echo "Conversion failed: " . $e->getMessage().'<br />';
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
//time of script
//sleep(1);
$timeC = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Full Process Time: {$timeC} $Found";
?>
CSV format:
"HeaderData1","HeaderData2","HeaderData3","HeaderData4","HeaderData5"
"DataA","DataB","DataC"
"Data2","Data2","Data2"
MySQL table format
id INT PK with AI and NN
column1 Varchar
column2 Varchar
column3 Varchar
Thank you for any help
Related
Forgive me, as I've not worked in php for years. I'm picking up some old code to get working again and I'm having a strange issue.
I'm writing in php with wordpress. As I am editing the code, I've noticed the php tag is closing after the following:
<?php
$databaseHost = "Localhost";
$databaseName = "testDB";
$databaseUser = "TESTUSER";
$databasePassword = "TESTPASS";
$coin_id = (isset($_POST['coin_id'])) ? $_POST['coin_id'] : '';
try {
$db = new PDO('mysql:host=' . $databaseHost . ';dbname=' . $databaseName . ';charset=utf8', $databaseUser, $databasePassword);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$request = "SELECT
_7UR_participants_database.city,
_7UR_participants_database.state,
_7UR_participants_database.country,
_7UR_participants_database.zip,
_7UR_participants_database.coin_id,
FROM _7UR_participants_database GROUP BY _7UR_participants_database.coin_id ASC";
$stmt = $db->query($request);
$item_info = $stmt->fetchAll();
} catch (PDOException $e) {
echo "Exception: " . $e->getMessage();
exit;
} // Try / catch end
?>
Everything after that > following $db- is not included in the php. The php tag is closing with that last >. Do I need to escape the character or something of that nature?
since your formatting is hard to undertand i rewrite it on my liking that it "migth" actually works
$dsn = "mysql:host=localhost;dbname=testDB;charset=utf8mb4"; // most cool kids use charset=utf8mb4
$options = [
PDO::ATTR_EMULATE_PREPARES => false, // turn off emulation mode for "real" prepared statements
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
];
try {
$dbh = new PDO($dsn, "TESTUSER", "TESTPASS", $options);
} catch (Exception $e) {
error_log($e->getMessage());
echo ("Error Code: " . $e->getCode() . "<br>"); // never use echo on public release build it would leak your database credential this is optional great for troubleshooting
echo ("Error Message: " . $e->getMessage() . "<br>");
exit('Something weird happened');//
}
$request = $dbh->prepare("SELECT
_7UR_participants_database.city,
_7UR_participants_database.state,
_7UR_participants_database.country,
_7UR_participants_database.zip,
_7UR_participants_database.coin_id,
FROM _7UR_participants_database GROUP BY _7UR_participants_database.coin_id ASC";
$request->execute([]); // you do not have something like this also i never put value since i don't know what your doing
$item_info = $request->fetchAll(); // store the fetched on $item_info also you need to indicate what data type you fetching by default it always PDO::FETCH_ASSOC
I am running a test to serialize an array using PHP, in order to update my database I use the PDO extension. I have tried changing the code a bit, but there is no error to be catched by the try block. I have other functions where I successfully updated other tables, the only diferense here is the PDO::PARAM_LOB line. Any help is greatly appreciated.
<?php
require("database.php");
try {
$test = array('15525');
array_push($test, '12345');
var_dump($test);
$stest = serialize($test);
var_dump($stest) ;
$dapartmentToUpdate = 3;
$result = $db->prepare("
UPDATE departments_employees
SET employee_id = ?
WHERE department_id = ?
");
$result ->bindParam(1,$stest,PDO::PARAM_LOB);
$result ->bindParam(2,$departmentToUpdate,PDO::PARAM_INT);
$result -> execute();
} catch (Exception $e) {
echo "Could not write to database for some odd reason";
exit;
}
?>
I must add that by taking the query directly to SQL makes it run just fine, so I must be missing something. The statement on SQL looks like
UPDATE departments_employees
SET employee_id = 'a:2:{i:0;s:5:"15525";i:1;s:5:"12345";}'
WHERE department_id = 3
config.php are just some environment constants, user, password and some other private stuff, database.php looks like this:
<?php
require_once ('config.php');
try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME .";port=" . DB_PORT,DB_USER,DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}
I am trying to import data from a CSV file to my table using PHP. I have tried using the exact same code without the backslash for ENCLOSED BY '"' on phpmyadmin and the import is successful.
I have also checked the user permissions for admin.
Here is my code:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$pdo = new PDO('mysql:dbname=mydatabase;host=localhost;charset=utf8', 'admin', 'admin');
$pdo->exec('SET CHARACTER SET utf8');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$query = $pdo->prepare("LOAD DATA LOCAL INFILE 'C:\feed.csv' IGNORE INTO TABLE tablename
fields terminated by ','
enclosed by '\"'
lines terminated by '\n'
IGNORE 1 LINES
(field1, field2, field3, field4)", array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
$query->execute();
$query->fetchAll();
} catch (PDOException $e) {
echo 'error: ' . $e->getMessage();
}
?>
When running this code I am seeing this error:
General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
I don't no what is wrong in your code but you can use my code it is help full to you I think
I have implement this code and it is tested code. I think it is very use full
You have follow some rule:-
1.your csv file according to database table name (ex: db table name is users then csv should be users.csv)
2.Your csv file's first row should be db table fields name (ex: Id, name etc) after the start your data entry
3.you can download data source class from :- http://code.google.com/p/php-csv-parser/ because i have require below the code: require_once 'CSV/DataSource.php';
<?php
ini_set('memory_limit','512M');
$dbhost = "localhost";
$dbname = "excel_import";
$dbuser = "root";
$dbpass = "";
$conn=mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db($dbname) or die("Unable to select database because: " . mysql_error());
require_once 'CSV/DataSource.php';
$filename = "users.csv";
$ext = explode(".",$filename);
$path = "uploads/".$filename;
$dbtable = $ext[0];
import_csv($dbtable, $path);
function import_csv($dbtable, $csv_file_name_with_path)
{
$csv = new File_CSV_DataSource;
$csv->load($csv_file_name_with_path);
$csvData = $csv->connect();
$res='';
foreach($csvData as $key)
{
$myKey ='';
$myVal='';
foreach($key as $k=>$v)
{
$myKey .=$k.',';
$myVal .="'".$v."',";
}
$myKey = substr($myKey, 0, -1);
$myVal = substr($myVal, 0, -1);
$query="insert into ".$dbtable." ($myKey)values($myVal)";
$res= mysql_query($query);
}
if($res ==1)
{
echo "record successfully Import.";
}else{
echo "record not successfully Import.";
}
}
I have a php file that imports a CSV file into my database. Since my hosting provider upgraded to PHP 5.4, I get an error on the line 'LINES TERMINATED BY ', but I am not sure why. Here is my code:
<?php
require('phpsqlajax_dbinfo.php');
$databasehost = $db_host;
$databasename = $db_name;
$databasetable = "tbl_csvImport";
$databaseusername=$db_user;
$databasepassword = $db_pass;
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "doecsv.csv";
echo $lineseparator;
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$pdo->exec("DELETE FROM `$databasetable`");
try {
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".preg_replace('/"[^"]+"/','',$pdo->quote($csvfile))." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
OPTIONALLY ENCLOSED BY ".$pdo->quote('"')."
LINES TERMINATED BY ".$pdo->quote($lineseparator)."IGNORE 1 LINES");
} catch (Exception $e) {
die ("CSV Parse Failed: ".$e->getMessage()." | Error on line: ".$e->getLine());
}
echo" Success. <br/>";
?>
Error Code: SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version | Error on line: 35
I found a workaround that works for me:
http://crewow.com/CSV-Importer-in-MySQL.php
This question already has answers here:
Import CSV file directly into MySQL
(6 answers)
Closed 9 years ago.
I have a CSV file that I'm wanting to import into a database, and then export the same data with omitted columns. How would I go about doing this? Any suggestions?
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator));
echo "Loaded a total of $affectedRows records from this csv file.\n";
?>