0

I get the following error. tried for 3 days to understand it, but no success:

PHP Parse error: syntax error, unexpected '{' in
/home/jharvard/vhosts/pset8/import on line 13. 

It's the very first curly brace. My code:

#!/usr/bin/env php
<?php

$sourcefile = $argv[1];

import($sourcefile);

/**
* fills database "places" with geo-data from a text-file
*/
function import($sourcefile) {  

    // file exists?
    if (!file_exists($sourcefile) {
        print("The source file doesn't exist!");
    }

    // file exists and readable?
    if (!is_readable($sourcefile) {
        print("The source file isn't readable (you may not have access to this file)!");
    }

    // open the source file
    $handle = $fopen("($sourcefile)", "r");

    // success?
    if ($handle === false) {    
        print("Source file not opened");
    }

    // source file accessible: iterate through and fill table places
    while (($places = fgetcsv($handle, 0, "\t")) {
            $insert = query("INSERT INTO places 
        (country_code, postal_code, place_name, admin_name1, admin_code1, 
        admin_name2, admin_code2, admin_name3, admin_code3, latitude,
        longitude, accuracy) 
        VALUES
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
        $places[0], $places[1], $places[2], $places[3], $places[4], 
        $places[5], $places[6], $places[7], $places[8], $places[9], 
        $places[10], $places[11])
        );
    }

    fclose($handle);
}

1 Answer 1

1

The problem is simple. Look at the offending line:

    if (!is_readable($sourcefile) {

Now check all of your parentheses pairings. This problem with mismatched parentheses occurs several times in your code, but I didn't look much further. No guarantees that there aren't more issues, but this will get you going again.

BTW, please spend a little time reviewing https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • That's exactly what I'm not seeing. There is one opening brace at the beginning of the function, then a pair for each if-statement. When i click one of a pair, I see the other is marked. And a closing brace at the end of the function. What's the mismatch, please? Trying the backtick (not available on my Thai/English-keyboard, have to switch to German) if (!file_exists($sourcefile) { print("The source file doesn't exist!"); } Even with the while function alone and exactly two braces under it, I get the same error.
    – swissfritz
    Commented Dec 29, 2015 at 10:28
  • Are you checking the pairing of the parentheses () or the curly braces {} ?
    – Cliff B
    Commented Dec 29, 2015 at 17:26
  • 1
    Thanks Cliff, I just didn't see it. English is only the 4th language I learned, so sometimes I tend not to see what's there but what I expect to see.
    – swissfritz
    Commented Dec 30, 2015 at 1:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .