UPDATE MySQL table from a CSV using PHP - php

I found and followed the directions contained within this StackOverflow thread: Update MySql Table from CSV using PHP
I've got an error somewhere that I'm unable to detect, I think there's a problem with my query, which works fine in actual MySQL but seems to not quite translate to PHP.
In short, I'm trying to UPDATE the value of several rows within a single table (catalog_product_entity_varchar) with CSV column $data[1], but only where certain skus are concerned AND attribute_id = 523 AND entity_id matches $data[0] of my CSV. Here's my code (actual PW/username, etc, obviously removed)
$con=mysqli_connect("localhost","username","password","some_db");
if (!$con){
die('Could not connect: ' . mysql_error());
}
if (($file = fopen("upload.csv", "r")) !== FALSE) {
while (($data = fgetcsv($file)) !== FALSE) {
$sql = "UPDATE catalog_product_entity_varchar
JOIN catalog_product_flat_1
ON catalog_product_flat_1.entity_id = catalog_product_entity_varchar.entity_id
SET catalog_product_entity_varchar.value='{$data[1]}'
WHERE catalog_product_entity_varchar.entity_id='{$data[0]}'
AND catalog_product_entity_varchar.attribute_id = 523
AND (catalog_product_flat_1.sku LIKE '%PR%'
OR catalog_product_flat_1.sku LIKE '%PT%'
OR catalog_product_flat_1.sku LIKE '%PF%')";
if (mysql_query($con,$sql)) {
echo "Updated!";
} else {
echo "Error updating " . mysql_error();
}
}
}
fclose($file);
It simply returns "Error updating" for every line of the spreadsheet. This query, when simply done in MySQL (without the PHP) and modified to have actual values instead of $data[1] or $data[0] works just fine. What am I missing?
If you're unclear of what I'm trying to achieve, I did post this question yesterday (trying to do it via pure mySQL) and there's more context here - https://stackoverflow.com/questions/21170245/updating-a-joined-table-in-mysql-from-a-csv

Wow.
So I feel stupid. Apparently mixing mysqli_connect and mysql_query doesn't work very well. Adding the "i" to the "mysql" of mysql_query solved it. Thanks for looking everyone!

Related

Populating mysql database with csv file using php

I'm trying to populate a mysql database with the contents of a .csv file using php. I'd like to do it using php code (no phpadmin). I consulted another thread on stackoverflow (populating database from csv file using php) but am still getting stuck. Here is my code:
$file = fopen("input.csv", "r");
while ($data = fgetcsv($file, $lineLength = 0, $delimiter = ",")) {
$added = "INSERT INTO Items VALUES(".$data.")";
if($connection->query($added) === TRUE) {
echo "Values successfully added!";
} else {
echo "Error inserting values: " . $connection->error;
}
Some context: earlier in the code, I create a database, connect to it, and create a table in the database. That part works fine. I just get an error when I try to populate the database from a .csv file. Here is the error message I get:
Notice: Array to string conversion in C:\xampp\htdocs\assignment8\init.php on line 64
Error inserting values: Unknown column 'Array' in 'field list'
I get this message 12 times, which corresponds with the number of rows in the .csv file I'm trying to import. "Line 64" corresponds with the line in my code that starts with "$added = INSERT INTO..."
Any help or suggestions are greatly appreciated.
You have to access to your column in the array
If your csv is something like this
Inside your while, you can access the values like:
echo $data[0] // prints 'Value 1'
So you might want to do something like...
$added = "INSERT INTO Items VALUES(".$data[0].")";

MySQL Execute fails query after insert of record on previous pass

This routine used to work until we moved to a much faster debian linux server.
This is a snippet of code that reads through a csv file and inserts records into a table if another record with the same manufacturers_name dosn't exist. What is happening is the first record is inserted and when another record is found the execute function fails to find the previous inserted record and adds the record instead of updating. If I run the same routine a 2nd time without emptying the table all the records are found and only update takes place. I thought maybe the issue was speed so I tried putting sleeps in the process but it had no affect. Any ideas?
Thanks
while (($data = fgetcsv($handle, 0, chr(9),chr(0))) !== FALSE) {
****** Setup stuff here *******
$msql_data_array = array('manufacturers_name' => $data[$manufacturer_sn]);
$sql = "select count(*) as total,manufacturers_id,manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_name = \"" . $data[$manufacturer_sn] . "\"";
$manufacturers = $db->Execute($sql);
if ($manufacturers->fields['total'] == 0) { <<<<<<<<<<<< This always returns 0 even if the record was just added by the previous operation(s)
***** Inserts a new record ******
} else {
***** Updates the current record *******
}
]
This looks like a simple .csv import. You could replace the PHP code with a command line MySQL call, like so:
mysqlimport --fields-terminated-by '\t' db_name import.csv
Ref: http://dev.mysql.com/doc/refman/5.7/en/mysqlimport.html

PHP - insert into mutiple tables, each table with identifcal fields

I'm going to explain with my best efforts what my goal is here. Everything I've searched for online hasn't been relevant enough for me to gain an idea.
First off, this is a PHP assignment where we have to load CSV files into a MySQL database.
Now, each table (total of 4) have the exact same field values. What I am trying to accomplish is using a for each loop that populates each table with the information from the CSV file. I know I can do this by having a while loop for each table and CSV file but I'm trying to go above the requirements and learn more about PHP. Here is my code for what I'm trying to accomplish:
$files = glob('*.txt'); // .txt required extension
foreach($files as $file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle,4048, ",")) !== FALSE) {
echo $data[0]; // making sure data is correct
$import = "INSERT INTO".basename($file)."(id,itemName,price) VALUES('$data[0]','$data[1]','$data[2]')";
multi_query($import) or die (mysql_error());
}
fclose($handle);
}
else {
echo "Could not open file: " . $file;
}
}
Each CSV file contains the id, itemName and price. Hopefully this is understandable enough. Thank you
The way you are importing data into MySQL is OK for small volume of data. However, if you are importing huge volumes(thousands of rows), the best way would be to import it directy into MySQL is by using infile. Fo example:
LOAD DATA LOCAL INFILE '/path/to/your_file.csv'
INTO TABLE your_table_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES
TERMINATED BY '\n' (id, itemName, price)
That's a smarter way to import your CSV data :)

MySQL query's suceed in PHPMyAdmin but not in the PHP Script itself

Just while playing around with Querying in PHP i ran into some trouble. The title of this post explains the problem. When i run a query in PHPMyAdmin the results will be different from the results i get in the PHP program itself. Here is the code i am using. Sorry if it looks a little odd i've been cutting and pasting things all over the place in a frantic attempt to get it working.
$connect = array('username'=>'root', 'host'=>'127.0.0.1', 'password'=>'');
$link = mysql_connect($connect['host'], $connect['username'], $connect['password']) or die('Error creating link: ' . mysql_error());
mysql_select_db('testing_pages', $link) or die('Error connecting to database: ' . mysql_error());
$sql = "SELECT `name` FROM `names`";
$query = mysql_query($sql, $link) or die('Query Failed! Check error:<br/><br/>' . mysql_error());
$query_2 = mysql_fetch_array($query);
$query = $query_2;
$loop = count($query);
$count = 0;
while($count <= $loop) {
echo $query[$count] . '<br/>';
$count++;
}
see, what im trying to get it to do is read all the names, pop it into an array, then print them out with a while loop. But it only seems to return 1 result and thats the first name in the databse. but when i run the EXACT query through phpmyadmin it will return every name in the database... Another odd thing, when using the 'count' function to get the number of values in the array is claims that there are 3 values, but during the loop it just prints out the first name, then for the second two it returns an 'Undefined index'.
Hope i dont seem like a noob right now... And i hope i explained everything well. Thanks to anyone who can help.
mysql_fetch_array fetches one row in the form of an array. Here are the docs.
And pay attention to that big warning message at the top of the page when you read the docs...

bulk data insert sql issue

i am importing a huge data from a csv file into my database, but the issue is , if there is a error in the sql , my insertion stops , thus making my bulk insertion useless.i have to go back , delete the uploaded data and remove that entry which is causing issue in my insertion and start again . i want programme to skip it and continue insertion, i know i have to apply try and catch, i have applied it in my algo but i cnt understand how to use so that it continues its insertion .
here is my code
$num=35; //number of columns
$dum=true; // a check
$sum=0;// count total entries
if (($handle = fopen($_FILES['file']['name'], 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ',')) !== FALSE)
{
if($dum)
{
for ($qwe=0; $qwe < $num; $qwe++) { //searching the columns exact position in case they have been changed
if($data[$qwe]=='ID')
{$a=$qwe;}
.
.
.
.
.else if($data[$qwe]=='PowerMeterSerial')
{$aa=$qwe;}
else if($data[$qwe]=='Region')
{$ab=$qwe;}
else if($data[$qwe]=='Questions')
{$ac=$qwe;}
}
}
if(!$dum)
{
for($qwe=0;$qwe<$num;$qwe++)
{
if($qwe==7||$qwe==8)
{
if($qwe==7){$asd=$data[$qwe];}
$data[$qwe]=date('Y-m-d h-i-s',strtotime($data[$qwe]));
}
}
$data[57]=date('Y-m-d ',strtotime($asd));try {
$sql="INSERT INTO pm (ID, .......... Questions, dateofdata ,Unsuccessful) VALUES ('$data[$a]','$data[$b]','$data[$c]','$data[$d]','$data[$e]','$data[$f]','$data[$g]','$data[$h]' ,'$data[$i]','$data[$j]','$data[$k]', '$data[$l]', '$data[$m]','$data[$n]','$data[$o]','$data[$p]','$data[$q]','$data[$r]','$data[$s]','$data[$t]','$data[$u]','$data[$v]','$data[$w]', '$data[$x]','$data[$y]','$data[$z]','$data[$aa]','$data[$ab]','$data[$ac]','$data[57]','$data[30]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error(). $sql);
}
}
catch (Exception $e)
{
echo'<br>'; echo $sql;
}
$sum++;
}$dum=false;
}
}
?>
kindly note there is no issue in uploading algorithme or sql , its when input data does not match the data type than sql generates a error , for that i am trying try and catch .. please help
Change your die() command to a print(). You'll see what the error was, and the script will move on to the next line.
Given the structure of your code, I'm guessing it'll blow up anytime you're inserting a string (particularly with quotes inside it), causing SQL syntax errors. You MUST pass each text field from your csv through mysql_real_escape_string() BEFORE you insert those values into the query string.

Categories