Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

PHP not parsing ' character

Hello, while trying to import data into a database from a CSV, PHP is not parsing lines that contatin '

		if ($_FILES["file"]["size"] > 0) {
			$file = fopen($filename, "r");
			[b]while (($getData = fgetcsv($file, 10000, ",", '', "\")) !== FALSE) {[/b] <- I added \ for the escape character but am receiving a 
Parse error: syntax error, unexpected 'INSERT' (T_STRING)


				$sql = "INSERT INTO data(field1,field2,field3,field4)
                   values ('" . $getData[0] . "','" . $getData[1] . "','" . $getData[2] . "','" . $getData[3] . "')";
				$result = mysqli_query($con, $sql);
				if (!isset($result)) {
					echo "<script type=\"text/javascript\">
							alert(\"Invalid File:Please Upload CSV File.\");
							window.location = \"index.php\"
						  </script>";

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

You need to double-up the slash:
while (($getData = fgetcsv($file, 10000, ",", '', "\\")) !== FALSE)
Avatar of Computer Guy
Computer Guy

ASKER

Thank you, though I am still having an issue with the CSV taking a like like this: Don't Stop Believin'
Here's some information showing how quotes work in PHP.
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html

If you can show us a link to little bit of the test data, we might be able to show you a working code example.  You can upload small files to E-E and we can test with your uploaded files.
>> Hello, while trying to import data into a database from a CSV
How was the CSV generated -- was it exported from Excel format (*.xls or *.xlsx) to CSV, or did you have some custom script (like a web application querying the DB) create the CSV?
CSV was created with Excel.
The fourth parameter (the enclosure) is currently an empty string.  Specify a double quote as the enclosure instead.

while (($getData = fgetcsv($file, 10000, ",", '"', "\\")) !== FALSE)

Open in new window

You can upload small files to E-E and we can test with your uploaded files.
Test data is worth its weight in gold.
Still not working, here is a CSV

The CSV file does not have an enclosure character.
testcsv.csv
... file does not have an enclosure character.
Yes, and that is a problem because we don't know whether the separator character is truly a separator or whether it is part of the data!  However if we trust that the separator character can never appear in the data, we can do something like this.
https://iconoun.com/demo/temp_audiotech520.php
<?php // demo/temp_audiotech520.php
/**
 * https://www.experts-exchange.com/questions/29001584/PHP-not-parsing-'-character.html#a42003791
 */
error_reporting(E_ALL);
echo '<pre>';

// OUR TEST DATA
$url = 'https://filedb.experts-exchange.com/incoming/2017/02_w06/1143981/testcsv.csv';

// ITS DESTINATION - YOU MIGHT WANT TO CHANGE THE FILE SUFFIX TO 'CSV' INSTEAD OF 'TXT"
$out = 'storage/temp_audiotech520.txt';

// READ THE DEFECTIVE CSV
$csv = file_get_contents($url);

// GET EACH LINE
$lines = explode(PHP_EOL, $csv);

// BREAK AND RECONSTRUCT THE LINES
$arr = [];
foreach ($lines as $line)
{
    $arr[] = explode(',', trim($line));
}

// CREATE A USABLE CSV
$fpw = fopen($out, 'w');
if (!$fpw) trigger_error("Unable to open $out", E_USER_ERROR);
foreach ($arr as $new)
{
    print_r($new); // SHOW THE WORK IN PROGRESS
    if (!fputcsv($fpw, $new)) trigger_error("Unable to write $out", E_USER_ERROR);
}
fclose($fpw);

echo PHP_EOL . '<a target="_blank" href="' . $url . '">Original</a>';
echo PHP_EOL . '<a target="_blank" href="' . $out . '">Redacted</a>';

Open in new window

Work Product: https://iconoun.com/demo/storage/temp_audiotech520.txt
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The resources here can help you find good PHP examples and learning materials.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Good database examples, with tested-and-working code and written explanations here.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

And a note about this code construct:
$result = mysqli_query($con, $sql);
if (!isset($result)) {

Open in new window

Because the mysqli_query() function return a value (check the man page to see what might be returned), and this value is assigned to the $result variable, isset() will always return TRUE.  Therefore the exclamation point, which is the negation operator, will make the value into FALSE during the evaluation of the expression in the if() statement.  The if() statement as written is meaningless.  A successful alternative would be something like this:
$result = mysqli_query($con, $sql);
if (!$result) {

Open in new window

Best of luck with your project, ~Ray
Thank you.