import glob text file from php to mysql - php

My glob code finds the file and attempts to insert it into the table but fails. I couldn't find anyone that is asking the same question. I will have many files that need to be inserted into the same table. It is a coma delimited text file.
Updated code*
The following code works as long as I rem the load data line. When I use the load data command manually, it works. I have not found a post example that has solved the problem "Why does the load data command not work?"
$con = new mysqli('localhost', 'username', 'password', 'schaadan_hash');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connected. ";
echo "<br />";
}
$upload = "LOAD DATA LOCAL INFILE 'ntlmhash2_1.txt' INTO TABLE ntlm2 FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\n'(`clear` , `hash`);";
mysqli_query($con,$upload);
?>

Related

Insert csv file into mysql table using php

I want to load a huge amount of data from a CSV file. I wrote this program to do it but when I execute the program it shows me an error. The error is:
file not found
If I echo the query then the file path that is shown is different than the mysql search for the file.
$fileName = $_FILES["file"] ["tmp_name"];
// Creating a temporary table to hold csv file data....
$tmp_tbl = "CREATE TABLE track LIKE trackingsite";
mysqli_query($this->conn, $tmp_tbl) or die( "Temporary table creation failed: " . mysqli_error($this->conn) );
$file_load = "LOAD DATA INFILE '$fileName' INTO TABLE track FIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' ";
echo $file_load;
echo '<br>';
echo '<br>';
mysqli_query($this->conn, $file_load) or die( "Can not insert data: " . mysqli_error($this->conn) );
I think you should use absoulute directly path to load the file.
Like 'for exmaple "C://xammp/htdoc" in $fileName

load data infile function with PHP

I have tried to import CSV file into MySQL database using PHP and MySQL load data Infile function.The query looks correct but still there is an error, while embedding it inside php code. Would you Please help me.
<?php
//connect to the database
$host='localhost';
$username='';
$password='';
$database_name='cricket';
$connection=mysql_connect($host,$username,$password,$database_name);
if(!$connection)
{
echo "no connection made";
}
//
else {
$query="LOAD DATA INFILE 'cricket.csv'
INTO TABLE cricket_batsman
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'";
mysql_query($query);
}
?>

Need to upload stats data from a csv file to sql database automatically every day?

Ok so im trying to use phpjobscheduler to update stats every day, It runs a command every day to open the php page and updates the db.
I have Googled and this is the code I managed so far:
<?php
$mysqli = new mysqli('localhost','xxxxx','xxxxx','xxxxx');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
$res = $mysqli->query($query);
if ($mysqli->error) {
try {
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $query", $msqli->errno);
} catch(Exception $e ) {
echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >";
echo nl2br($e->getTraceAsString());
}
}
mysqli_close($con);
?>
This however is not working it gives me a blank page,
and nothing is updated on the SQL-database?
it connects to the db
(enter wrong details it shows incorrect user details)
but is not updating the db ? and shows no errors?
Please assist?
all I need to do is to update the db with the data from the .csv file every day at 1am.
You need to escape the double quote at ENCLOSED BY part. Without that, you terminated your string.
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
Note
Use error_reporting(E_ALL), and ini_set('display_errors', 1); to cactch your errors while developing.

mysql load data infile through php script - not working

I am trying to use the mysql LOAD DATA LOCAL INFILE to get some csv data into my mysql database through a php script using mysqli. This is what my sql string looks like:
LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv' INTO TABLE dashmaker.HHdata FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
This is what my php script looks like:
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","[user]","[password]","[database]");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($sql, $con);
if (mysql_affected_rows() == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysql_error();
};
echo $message;
mysqli_close($con);
I found that I needed to set the mysql my.cnf to include local-infile under [mysql] and [mysqld] - so I have done that.
When I run the sql query through the shell it works. When I try to do it through the php script the error message ($message) I now get says:
The user update failed: Access denied for user ''#'localhost' (using password: NO)
One weird thing is that it doesn't show any user name before the #'localhost'. Can't understand why. Besides this, I use the same connection setting to run regular SELECT queries from the database using php scripts. The user also has FILE privileges.
I have searched extensively but haven't found anything that can explain what's going on. Any advice would be much appreciated.
You're mixing MySQL APIs with mysql_ and mysqli_ functions in a few instances.
mysql_affected_rows()
mysql_error()
They do not mix together; use mysqli_ exclusively.
Plus, you're not using brackets in [user] etc, are you? That is MSSQL syntax, remove them.
Plus, in mysqli_, DB connection comes first, invert these $result = mysqli_query($sql, $con); to read as $result = mysqli_query($con, $sql);
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($con, $sql);
if (mysqli_affected_rows($con) == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysqli_error($con);
};
echo $message;
mysqli_close($con);

How do I use php to create a database with table and info using a backup file?

How do I use php to create a database with table and info using a backup file?
so far my code is
<?
header("content-type:text/plain");
$serv= mysql_connect('localhost', 'root', 'xxxx');
if($serv){
wText("Server connection created");
}
else{
wText("Server connection failed");
}
$sql ="CREATE DATABASE tf2faq";
if(#mysql_query($sql,$serv)){
wText("Database tf2faq created");
}
else{wText("Database tf2faq create failed");}
$sql = 'CREATE TABLE `tf2faq`.`loadoutitems` (`item` TEXT NULL, `class` TEXT NULL, `type` TEXT NULL, `replaces` TEXT NULL, `itempos` TEXT NULL, `modpos` TEXT NULL, `modneg` TEXT NULL, `imgurl` TEXT NULL, `notes` TEXT NULL) ENGINE = MyISAM';
mysql_query($sql,$serv);
if(#mysql_query($sql,$serv)){
wText("table loadoutitems created");
}
else{
wText("table loadoutitems create failed");
}
$sql="LOAD DATA INFILE 'loadoutitmsfx.csv' INTO TABLE `tf2faq`.'loadoutitems'";
if(#mysql_query($sql,$serv)){
wText("Data Load Passed");
}
else{
wText("Data Load Failed");
}
function wText($txt){
echo $txt . "\n";
}
?>
it fails on the LOAD DATA line, not sure why.
edit in my code i removed the "#" in there and it still says the file load failed
Where is loadoutitmsfx.csv? The file needs to be on the computer where the database is running. You might try specifying the whole path to it. Something like "/home/jim/loadoutitmsfx.csv".
Do the number of columns from your csv file and the table match up?
You probably need to specify some more options to get csv working.
LOAD DATA INFILE '/home/jim/loadoutitmsfx.csv' INTO TABLE tf2faq.loadoutitems
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\'
LINES TERMINATED BY '\r\n'
Additionally don't use #.
LOAD DATA INFILE is disabled on most MySQL servers, as it doesn't work with remote database servers and requires the data file to be readable by the database server. Parse the file yourself and generate SQL statements.

Categories