473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

using heredoc for SQL statements

Tom
I've used heredocs for single SQL statements without a problem. Also,
I've tried this using the SQL form on PhpMyAdmin and it works so I
conclude it should work in PHP.

Problem: getting syntax error with following SQL statement:

$_sql = <<<SQLDOC
LOCK TABLES `table_name` WRITE;
INSERT INTO `table_name` ('col_a', 'col_b') VALUES ('value_a1',
'value_a2');
INSERT INTO `table_name` ('col_a', 'col_b') VALUES ('value_b1',
'value_b2');
UNLOCK TABLES;
SQLDOC;

Error Message:

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '; INSERT INTO `table_name`..."

The manual didn't help me. I suspect maybe it has something to do with
the way the semicolon is parsed? What am I missing?

Thanks in advance,

Tom

Jan 17 '06 #1
5 5630
On 2006-01-17, Tom <kl******@gmail.com> wrote:
'value_a2');
INSERT INTO `table_name` ('col_a', 'col_b') VALUES ('value_b1',
'value_b2');
UNLOCK TABLES;
SQLDOC;

Error Message:

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '; INSERT INTO `table_name`..."

The manual didn't help me. I suspect maybe it has something to do with
the way the semicolon is parsed? What am I missing?


This is obviously a (My)SQL syntax error, thus you need to search in that
manual. Notice that there are no quotes around the column names:
http://dev.mysql.com/doc/refman/5.0/en/insert.html

INSERT INTO table (cola, colb) VALUES ('val1', 'val2');

In case you are using reserverd keywords as a table or column name, you should
use ``` to inform MySQL about that.

INSERT INTO `table` (`cola`) VALUES ('vala');

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Jan 17 '06 #2
Tom
Just noticed another syntax error -- but this wasn't in my original
code and isn't the problem. Nevertheless, statements in code above
should read:

INSERT INTO `table_name` (`col_a`, `col_b`) VALUES ('value_a1',
'value_a2');

or even:

INSERT INTO `table_name` VALUES ('value_a1', 'value_a2');

which produces in my script SQL syntax error:

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '; INSERT INTO `table_name`..."

Thanks again,

Tom

Jan 17 '06 #3
zoe
You're right: your use of heredocs is fine, and the semicolon is to
blame. Although you can group several queries together using the
command
line MySQL program, and using PhpMyAdmin, it doesn't work this way in
PHP. You have to submit only one query at a time, so that you can
process the result of each query seperately (even if the query won't
give you a particularly useful result, it still has to be seperated).

Zoe.

Jan 17 '06 #4
Tom said the following on 17/01/2006 03:27:
I've used heredocs for single SQL statements without a problem. Also,
I've tried this using the SQL form on PhpMyAdmin and it works so I
conclude it should work in PHP.

Problem: getting syntax error with following SQL statement:

$_sql = <<<SQLDOC
LOCK TABLES `table_name` WRITE;
INSERT INTO `table_name` ('col_a', 'col_b') VALUES ('value_a1',
'value_a2');
INSERT INTO `table_name` ('col_a', 'col_b') VALUES ('value_b1',
'value_b2');
UNLOCK TABLES;
SQLDOC;

Error Message:

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '; INSERT INTO `table_name`..."


mysql_query() doesn't support multiple SQL statements separated by
semi-colons. You'll either need to separate your statements into
separate mysql_query() calls, or use the mysqli_multi_query() statement,
if it's available.

--
Oli
Jan 17 '06 #5
Tom
Thanks for the responses. I finally found this:

http://www.php.net/manual/en/functio...uery.php#37870

The PHP manual isn't explicit on the point, though it does note, "The
query string should not end with a semicolon."

Below is my variation on the function provided in the comment:

/* fx mysql_query_batch
source: http://www.php.net/manual/en/functio...uery.php#31381
*************************************************/
function mysql_query_batch($query, $as_transaction=TRUE)
{
// *** DATA

# internal
$_SPLIT = array();
$_statement = '';

# return
$query_result = 0;
// *** MANIPULATE

# transaction-safe query
if ( $as_transaction )
{
$query = 'START TRANSACTION;' . $query . '; COMMIT;';
}

# split query
$_SPLIT = preg_split("/[;]+/", $query);

# process statements one-by-one
foreach ( $_SPLIT as $_statement )
{
$_statement = trim($_statement);

if ( !empty($_statement) )
{
# try query
$query_result = mysql_query($_statement);

# catch
if ( !$query_result )
{
trigger_error('MySQL error number '.mysql_errno().': '.mysql_error());
break;
}
}
}
// *** RETURN

return $query_result;

} # end Fx
/*______________________________________________*/

Not extensively tested, but it's met my demands thus far.

Incidentally, I dug out the phpmyadmin function -- it parses the
textarea post character-by-character and splits up the statements.

Jan 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: bigoxygen | last post by:
Is it possible to invoke a function from inside heredoc? Thanks
7
by: flupke | last post by:
Hi, i have php script where i use a heredoc type of variabele to print out some html. In the heredoc, i use the values of several other vars. php snippet: $div=<<<END_DIV <DIV...
2
by: Yves Touze | last post by:
Hi All, I'm trying to migrate from SQL Server 7.0 to SQL Server 2000. I've got some ASP page which call VB components that retrieve shaped recordsets from SQL Server using the MSDATASHAPE...
6
by: bill | last post by:
Thanks to those that taught me to use heredoc to embed html in php. I don't want to start the html in php vs php in html discussion again., heredoc works a treat for that which I am doing...
2
by: Matt F | last post by:
I can't seem to get heredoc to populate correctly with variables through a form. <textarea name="Template" rows="10" cols="80">Template Here</textarea> Contents could be something like: I want...
7
by: Jeff | last post by:
I have this: $content = <<<TD $D TD; that fails silently and stops php even though errors are turned on
2
by: Adam Cantrell | last post by:
How could I count the number of newlines in a heredoc variable? This outputs 0, but I thought heredoc preserved newlines? <? $test = <<<END <a href="test5.php">adam</a> <p> Hi There </p>...
2
by: someusernamehere | last post by:
hi, I have some heredoc on this way: $foo = <<<bar <form action="index.php" method="POST" name="user"> ............................................................................. HTML code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.