-1

Okay this will be my last post here for today, have asked you guys enougth questions about why my terrible code dosnt work :)

I am trying to run a query to update all values of a column with a text documents contents. My database looks like follows:

Attribute_id | value
64 | Data.txt
109 | other data
64 | Data2.txt
109 | other data

If you look at my script you can see what I am trying to do, for now I am echoing the query the end result is here: http://dev.central-pet-supplies.co.uk/test.php

it is returning UPDATE mage_catalog_product_entity_text SET value= 'Array' WHERE value = '13685.txt' however array should be the text files contents

Here is the script:

<?php

mysql_connect ("localhost","cpsdev_mage1","********"); 
mysql_select_db ("cpsdev_mage1"); 

$sql = "select value from mage_catalog_product_entity_text WHERE Attribute_id = 64"; 
$result = mysql_query ($sql) or die($myQuery."<br/><br/>".mysql_error()); 

while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$lines = file('http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']);
$query3 = "UPDATE mage_catalog_product_entity_text " . " SET value= '" . $lines .  "'         WHERE value = '" . $row['value'] . "'";
echo $query3 ."<br>";
}

?>

Edit: changed file to file_get_contents and now working.

Simon Staton
  • 4,345
  • 4
  • 27
  • 49
  • Please note: be aware that the `mysql_xx()` functions are considered obsolete and are being deprecated. You should avoid using them if at all possible. The PHP manual recommends switching to either the `mysqli_xx()` functions or the PDO library. See here for more info: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php – SDC Oct 30 '12 at 13:06

2 Answers2

0

file() reads in the entire file with each line as a new entry in an array. Since you don't want that, you can either join the lines together using implode() by putting

$lines = implode( "\n", $lines);

After:

$lines = file('http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']);

Or, by using file_get_contents() instead of file(), which will return the entire file's contents as a string, instead of an array:

$lines = file_get_contents( 'http://dev.central-pet-supplies.co.uk/media/import/' . $row['value']);
nickb
  • 59,313
  • 13
  • 108
  • 143
0

You dont show where $lines comes from but its an array i assume of lines in the text file. Use something like file_get_contents to get the contents of the text file as a single string instead.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114