I want to upload large CSV document into mysql database can anyone help me out, the table consist of 10 fields but i want to upload into only 5 fields of the table without a heading. I want this done with php in the browser
$filename = $_FILES['sel_file']['tmp_name'];
<?php
if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['name'])){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'cbt_software';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
mysql_select_db('cbt_software') or die(mysql_error());
//Process the CSV file
$handle = fopen($_FILES['file']['name'], "r");
$data = fgetcsv($handle, 1000, ";");
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$att0 = mysql_real_escape_string($data[0]);
$att1 = mysql_real_escape_string($data[1]);
$att2 = mysql_real_escape_string($data[2]);
$att3 = mysql_real_escape_string($data[3]);
$att4 = mysql_real_escape_string($data[4]);
$sql = "INSERT INTO `course_reg` (`coursecode`,`coursename`,`coursedescription`,`coursemaster`,`courselevel`)VALUES ('$att0','$att1','$att2','$att3','$att4')";
mysql_query($sql) or die(mysql_error());
}
mysql_close($link);
echo "CSV file successfully imported.";
}
else{
die("You shouldn't be here");
}
?>
At first this imported all the field from the csv into just one field in the database and after i tampered with the code it is not recognicing it a s a CSV file.
If you have the appropriate permissions, you can do so directly in MySQL with the LOAD DATA INFILE command, see http://dev.mysql.com/doc/refman/4.1/en/load-data.html or the mysqlimport utility, see http://dev.mysql.com/doc/refman/4.1/en/mysqlimport.html
Both methods will allow you to specify which columns the data should go in, for instance:
LOAD DATA INFILE 'myfile.txt' INTO TABLE 'mytable' (col1, col2, col3, ...)
or
mysqlimport --columns='col1,col2,...' tablename.csv
If you intend to do it from PHP, you should be able to read each line of the CSV file and execute an appropriate SQL INSERT query naming the appropriate columns (although that will not be as efficient as doing it directly in MySQL).
EDIT: I should add that you haven't mentioned what you've tried so far or what you're finding difficult; if you're stuck on something in particular, rather than just looking for suggestions on how to go about doing it, please update the question to say so.
Related
I would like to make a program for my website that will make a csv report of the data entered in my database. The problem is that I don't have a clue how. I know it's possible due to my searching but all of them did not really dumb it down to be enough for me to understand. Can someone please push me into the right direction?
Date | Entries
2016 | 99 Records
2017 | 50 Records
I'd like for this to show in the excel file along with the column name and the rows in order.
First make a connection with your database:
$databaseName = '';
$databaseHostname = 'localhost'
$databaseUsername = '';
$databasePassword = '';
$pdo = new PDO(
'mysql:host=' . $databaseHostname . ';dbname=' . $databaseName,
$databaseUsername,
$databasePassword);
Now get all data from the table
$tableName = 'test';
$statement = $pdo->prepare('SELECT * FROM ' . $tableName);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
Now create a csv file
$fileName = 'test.csv';
$csvFile = new SplFileObject($fileName, 'w');
And add the data to the csv file.
foreach ($results as $row) {
$csvFile->fputcsv($row, ";");
}
I have data in a .txt file.
4654664
6545645646
54564121452
564754412
5456545
I want to insert this data in db using php
<?php
$host= "localhost";
$user= "infdgfg";
$pass= "98fgfgdghf6";
$db="xxxxx";
$connect= mysql_connect($host,$user,$pass);
if (!$connect)die ("Cannot connect!");
mysql_select_db($db, $connect);
$file = fopen("oxxxkxxn.txt","r");
while(! feof($file))
{
$sql = "INSERT INTO xxxxx( xxxxx ) VALUES ('($file)')"; //Insert every read line from txt to mysql database
mysql_query($sql);
}
fclose($file);
?>
but this is not working.
(Resource id #4)
(Resource id #4)
(Resource id #4)
this error.
fopen returns a file pointer (that's your Resources).
http://php.net/manual/en/function.fopen.php
After opening your file you then need to use fgets to get the data line by line.
http://php.net/manual/en/function.fgets.php
So you would do something like:
$sql = "INSERT INTO xxxxx( xxxxx ) VALUES ('" . fgets($file) . "')";
I want to import data stored in a .csv file into mysql via php. The LOAD DATA INFILE query does not seem to work and i managed to write a code that will import the records one by one. Here is the code:
<?php
$arr = array(array(),array());
$num = 0;
$row = 0;
$handle = fopen("./import.csv", "r");
while($data = fgetcsv($handle,1000,",")){
$num = count($data);
for ($c=0; $c < $num; $c++) {
$arr[$row][$c] = $data[$c];
}
$row++;
}
$con = mysql_connect('localhost','root','password22');
mysql_select_db("security",$con);
for($i=1; $i<$row; $i++){
$sql = "INSERT INTO sls VALUES ('".$arr[$i][0]."','".$arr[$i][1]."','".$arr[$i][2]."','".$arr[$i][3]."','".$arr[$i][4]."','".$arr[$i][5]."')";
mysql_query($sql,$con);
}
?>
The problem is that I have about 300 000 records to import and when the records get too much, no record gets imported into the database and I get an error message. Is there anyway I can import the data faster or are there any similar statements like LOAD DATA INFILE I can use in PHP?
This may help you out
<?php
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('filename.xls');
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql="CREATE TABLE tablename(
quote_number VARCHAR(100),
line_no VARCHAR(100),
item_no VARCHAR(100),
name VARCHAR(100),
unit VARCHAR(100),
rm VARCHAR(100),
capex VARCHAR(100))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table tablename created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
for($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
{
//$sno = $data->sheets[0]["cells"][$x][1];
$quote_number = $data->sheets[0]["cells"][$x][1];
$line_no = $data->sheets[0]["cells"][$x][2];
$item_no = $data->sheets[0]["cells"][$x][3];
$name = $data->sheets[0]["cells"][$x][4];
$unit = $data->sheets[0]["cells"][$x][5];
$rm = $data->sheets[0]["cells"][$x][6];
$capex = $data->sheets[0]["cells"][$x][7];
$res = mysqli_query($con,"INSERT INTO tablename
(quote_number, line_no, item_no,name,unit,rm,capex) VALUES ('$quote_number','$line_no','$item_no','$name','$unit','$rm','$capex')");
mysqli_close($res);
}
?>
you can download reader.php file from here
Since you have not shared the LOAD DATA INFILE I assume most probably the issue is with FILE Privileges
You have to GRANT FILE privileges to the person loading the file(Mysql user connecting through username/password from php). Sth like this:
GRANT FILE on *.* TO 'mysql_user'#'localhost' ;
Since FILE is global privilege you cannot localize that to a specific database e.g dbname.* , just to note. To run the above command itself you should login to mysql from command line as root or as a user who has GRANT privileges.
Once that is done you should able to load the file using LOAD DATA INFILE. IF you are trying this in shared-hosting environment, your chances are very little. In that case you have to use the above method you tried , read the file, split the each line ,validate the line , insert into db table.
I have a very long list in plain text which I need to insert into a table my database. Do I have to manually input each line of my plain text document or is there a way to insert long lists into independent rows in a table using a query?
I have a table with 2 columns, id and club_name, club_name is the list which is plain text in a notepad document.
You can use LOAD DATA, e.g.:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet
-> LINES TERMINATED BY '\r\n';
You can also use the multi-row insert syntax (if you are using InnoDB tables), like this:
INSERT INTO yourtable VALUES (1,2), (5,5), ...
You can load data into mysql using the LOAD DATA INFILE command.
Here is the documentation...
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
$file = file("content.txt"); //read file line by line
foreach ($file as $val) {
if (trim($val) != '') { //ignore empty lines
mysql_query("INSERT INTO xxx SET club='" . $val . "'");
}
}
Here's a quick PDO based example, but MySQL's LOAD DATA INFILE command may be a much better option if you don't need to manipulate the source data (trim, normalise etc).
<?php
$conn = new PDO($dsn, $username, $password);
$stmt = $conn->prepare('INSERT INTO table VALUES (NULL, ?)');
foreach(file('list.txt') as $club) {
$stmt->execute(array($club));
}
Anthony.
Here is how to do it with php, mysqli and a text file that has each entry on a single line:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$file = file("list.txt"); //read file line by line
foreach ($file as $val) {
if (trim($val) != '') { //ignore empty lines
$sql = "INSERT INTO `db`.`table` (`column`) VALUES ('$val');";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
This is based off of DanFromGermany's answer but updated to use mysqli.
Insert the whole notepad content to a column of type "text"
So I am trying to make this website that takes in the values that are given to it by a text document and pout them into a mySQL database. The code I am running in my php doesn't give me any syntax errors, but the values aren't added to the database tables.
$upload = new mysqli('localhost', 'uMoviesRoot', $_POST['password1']);
if (mysqli_connect_errno()) {
echo "There as an error.";
}
else {
mysql_select_db("localhost");
$file= fopen($_FILES['Upload']['tmp_name'], 'r');
while(! feof($file)){
$line = fgetcsv($file, 999);
if ($line[0] == "movie") {
mysql_query("INSERT INTO movies (movie, year) VALUES ($line[1], $line[2])");
$movieCount++;
$lastMovie = $line[1];
}
Just some background, I have created the tables in mySQL (using MySQL workbench) and made a schema named movies. There are tables named actors(2 columns), directed_by (2 columns), directors(1 column), movies(2 columns), and performed_in (3 columns). I only put one of these additions in the code just to make it shorter (since all of the ifs do the same thing).
Is this a problem with my PHP code?
You should mysql_select_db("movies");, localhost is your server address and not the database name.
UPDATE (not testet but this should work):
$upload = new mysqli('localhost', 'uMoviesRoot', $_POST['password1']);
if (mysqli_connect_errno()) {
echo "There as an error.";
} else {
mysql_select_db("movies");
$file= fopen($_FILES['Upload']['tmp_name'], 'r');
while(! feof($file)){
$line = fgetcsv($file, 999);
if ($line[0] == "movie") {
mysql_query("INSERT INTO movies (movie, year) VALUES ('$line[1]', '$line[2]')");
$movieCount++;
$lastMovie = $line[1];
}
}
}
You could also output mysql_error to see the errors.
If you have values from user input, you should have a look at Prepared Statements to avoid SQL injection, etc.