如何执行mysql命令DELIMITER [英] how to execute mysql command DELIMITER

查看:199
本文介绍了如何执行mysql命令DELIMITER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很大的问题(对我来说)

I have huge problem (for me)

我需要从PHP执行MySQL命令DELIMITER |,但是mysql_query()失败并出现错误.我发现mysql_query()不支持使用DELIMITER,因为此命令仅在MySQL控制台中有效

I need to execute the MySQL command DELIMITER | from PHP, but mysql_query() fails with an error. I found that mysql_query() doesn't support the use of DELIMITER, because this command only works in MySQL console

但是当我打开phpMyAdmin时,可以在SQL选项卡上更改DELIMITER的选项,它可以工作,但是我不知道如何.

But when I open phpMyAdmin there is an option to change DELIMITER on the SQL tab and it works, but I don't know how.

是否可以从PHP更改定界符?在调用使用多个;CREATE TRIGGER命令之前,我需要这样做.

Is it possible to change delimiter from PHP? I need it to do before calling a CREATE TRIGGER command that uses several ;.

推荐答案

您可能不需要更改定界符.

在CLI中需要定界符来告知SQL语句的结束位置,因为CLI会继续读取并执行更多的语句,直到您告诉它停止为止(例如,使用exit或Control-D).但是它实际读取的只是字符流.它以某种方式需要弄清楚一个语句在哪里结束而下一条语句在哪里开始.这就是定界符的作用.

The delimiter is needed in the CLI to tell where the SQL statement ends, because the CLI is going to keep reading and executing more statements until you tell it to stop (e.g., with exit or Control-D). But what it actually reads is just a stream of characters; it somehow needs to figure out where one statement ends and the next starts. That's what the delimiter does.

在PHP中,每个函数调用执行一个语句.在一个函数调用中不能有多个语句,因此不需要分隔它们的方法.该语句是整个字符串.旧的mysql_query以及更新的mysqli_query和PDO都是如此.当然,如果您真的想将多个查询传递给一个函数,则有mysqli_multi_query.

In PHP, each function call executes one statement. There can't be multiple statements in one function call, so there is no need for a way to delimit them. The statement is the entire string. This is true of the old mysql_query as well as the newer mysqli_query and PDO. Of course, there is mysqli_multi_query if you really want to pass multiple queries to one function.

对于存储过程/触发器/函数/等,可以有多个语句,但这由MySQL本身处理(始终为;,AFAIK).因此,就PHP而言,这仍然是一个声明.

In the case of a stored procedure/trigger/function/etc., there can be multiple statements, but that's handled by MySQL itself (and is always ;, AFAIK). So as far as PHP is concerned, that's still one statement.

您在phpMyAdmin中看到的定界符设置可能已用于将语句分开,并且可能已在PHP代码中完成.之所以必须这样做,是因为它接受由多个语句组成的用户输入,但是每个函数调用只能传递一个语句. (我没有检查phpMyAdmin代码来完全确定这一点.)

The delimiter setting you're seeing in phpMyAdmin is probably being used to split statements apart, and is probably being done in PHP code. It has to do this because it is accepting user input consisting of multiple statements, but must pass only one statement per function call. (I haven't checked the phpMyAdmin code to be completely sure of this).

这篇关于如何执行mysql命令DELIMITER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆