Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT INTO SQL Heredoc correct syntax

Tags:

php

heredoc

mysql

I'm trying to use heredoc syntax with a INSERT INTO MySQL command, into a database.

The problem I'm facing is with the variable $tPerson_SQLinsert, which is a query.

Here's my code:

$tPerson_SQLinsert = <<<SQL
    INSERT INTO tPerson (Salutation, FirstName, LastName, Tel, companyID)
    VALUES ("$Salutation", "$FirstName", "$LastName", "$Tel", "$companyID")
    SQL;

Well, there´s probably something wrong with that syntax.

Because I´m getting this Parse error:

 Parse error: syntax error, unexpected end of file in F:\wamp\www\forms\personInsert.php on line 83

That is just that end tag of php ?>.

What would be the correct syntax for the heredoc?

Thanks.

like image 312
Marcelo Noronha Avatar asked Oct 23 '13 11:10

Marcelo Noronha


1 Answers

It will work when you format it like so:

$tPerson_SQLinsert = <<<SQL
    INSERT INTO tPerson (Salutation, FirstName, LastName, Tel, companyID)
    VALUES ("$Salutation", "$FirstName", "$LastName", "$Tel", "$companyID")
SQL;

I.e. the final delimiter should start in column 1.

See docs (in the warning block): https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

like image 176
aderuwe Avatar answered Sep 22 '22 06:09

aderuwe