Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php - How to get mysqli working with DELIMITERs in SQL statements?

I'm using mysqli and trying now to get a view from an SQL code snippet (generated by MySQL Workbench) into a database.

$query = <<<QUERY
DROP VIEW IF EXISTS `myview` ;
SHOW WARNINGS;
DROP TABLE IF EXISTS `myview`;
SHOW WARNINGS;
DELIMITER $$
CREATE OR REPLACE VIEW `myview` AS

...view definition...

$$
DELIMITER ;

;
SHOW WARNINGS;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
QUERY;

$result = mysqli_multi_query($dbConnection, $query);

This doen't work. No errors, the statement seems just to be ignored. It only works, if I remove the delimiter definitions (lines DELIMITER $$, $$, and DELIMITER ;).

Why is it not working? What can/should I do to use delimiters in SQL statements passed to mysqli functions and methods?

Thx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the source that helped me understand this... http://zend-framework-community.634137.n4.nabble.com/Problems-changing-the-sql-end-of-statement-delimiter-tp2124060p2124276.html

DELIMITER is not an actual MySQL Statement.

I believe it is just something that some MySQL clients have implemented to help shipping a bunch of sql statements at the same time.

mysqli driver does not implement this functionality.

So, this should work.

$query = <<<QUERY
DROP VIEW IF EXISTS `myview` ;
SHOW WARNINGS;
DROP TABLE IF EXISTS `myview`;
SHOW WARNINGS;

CREATE OR REPLACE VIEW `myview` AS

...view definition...

;
SHOW WARNINGS;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
QUERY;

$result = mysqli_multi_query($dbConnection, $query);

I ran into the same problem, with the same mysqli driver, with multi_query function (using delimiters while creating procedures) and removing the DELIMITER from my SQL worked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...