Home>

Csv file data (lineid and usernamme) on EC2 is inserted into RDS with PHP.
I would like to confirm that the usage of baindvalue is correct.
As a premise, the csv file has been read, and the contents have been confirmed by debugging.
$file [0] contains lineid and $file [1] contains username.
The connection to DB has also been completed.
And when you run the code
"PHP Parse error: syntax error, unexpected end of file in /home/ec2-user/index.php on
line 40 "will come back.

I looked up and used it, but it's a good thing, but I can't use it.
Is it correct to recognize that the variable that stores the value I want to put in the second subtraction number of bindvalue?

I would appreciate any advice.

Read CSV file
try {
    $file = new SplFileObject ('/ tmp/winneyuser.csv');
    $file->setFlags (SplFileObject :: READ_CSV);
} catch (RuntimeException $e) {
    throw $e;
}
Code
DB connection
try {
        $pdo = new PDO (
            'mysql: host = *********,
            '************',
            '***************',
            [
                PDO :: ATTR_ERRMODE =>PDO :: ERRMODE_EXCEPTION,
                PDO :: ATTR_DEFAULT_FETCH_MODE =>PDO :: FETCH_ASSOC,
            ]);
} catch (PDOException $e) {
        header ('Content-Type: text/plain;charset = UTF-8', true, 500);
        exit ($e->getMessage ());
 }
 header ('Content-Type: text/html;charset = utf-8');
Import to DB using
bindvalue
$line_id = $file [0];
$username = $file [1];
    foreach ($file as $files) {
        $stmt = $pdo->prepare ("INSERT INTO users (line_id, name) VALUES (: lineid,: username)");
        $stmt = $pdo->prepare ($sql);
        $stmt->bindValue (': line_id', $line_id, PDO :: PARAM_STR);
        $stmt->bindValue (': username', $username, PDO :: PARAM_STR);
        $stmt->execute ();
        $pdo->commit ();
    }
  • Answer # 1

    I think it is better to put the whole amount if it is this amount, because it will be difficult to see if it breaks down and put the continuous code or whether it is really moving code or reprint error.

    For now, parseError is around here

    'mysql: host = *******
                '************',

    'mysql: host = *********',
                '************',

    The rest

    $stmt = $pdo->prepare ("INSERT INTO users (line_id, name) VALUES (: lineid,: username)");
           // $stmt = $pdo->prepare ($sql);// ← Delete because $sql does not exist and an error occurs

    Is it around?

    Basic measures

    Syntax errors are easier to find in IDEs and editors, so
    It is recommended to search withPHP IDEand prepare the development environment.

  • Answer # 2

    What if you actually move it and check the behavior?

  • Answer # 3

    It was a typo that erased the closing parenthesis of the try statement.
    It was solved by adding.

Related articles