为什么给 mysqli_query 时换行符消失了?
Why do the line breaks dissapear when given to mysqli_query?
当我将它粘贴到相关数据库的 phpmyadmin 查询 window 中时,这个用于插入触发器的查询有效。但是当我尝试使用 mysqli_query 将它查询到数据库时,换行符似乎消失了导致它失败。
代码:
$TriggerQwry = 'Delimiter $$
DROP TRIGGER IF EXISTS sign_chat;
$$
CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat FOR EACH ROW BEGIN
IF (INSTR(NEW.message,"(") != 1) THEN
SET NEW.message = CONCAT("(MC) ", NEW.message);
END IF ;
END ;';
$DBConnect = mysqli_connect("localhost", "root", "************");
mysqli_set_charset($DBConnect, "utf8");
mysqli_select_db($DBConnect, 'minecraft_xenforo');
$QueryResult = mysqli_query($DBConnect, $TriggerQwry);
if($QueryResult)
{
$result = mysqli_affected_rows($DBConnect);
}
else
{
$result = 0;
echo "<p>Unable to execute the query.[".$TriggerQwry."]</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect) . "</p>";
}
echo "Result[$Result]";
mysqli_close($DBConnect);
输出:
Unable to execute the query.[Delimiter $$ DROP TRIGGER IF EXISTS sign_chat; $$ CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat FOR EACH ROW BEGIN IF (INSTR(NEW.message,"(") != 1) THEN SET NEW.message = CONCAT("(MC) ", NEW.message); END IF ; END ;]
Error code 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Delimiter $$ DROP TRIGGER IF EXISTS sign_chat; $$ CREATE ' at line 1
我还尝试在 TriggerQwry 字符串上使用全引号 "" 和 \n 以及 \n\r 和 \r\n,但结果都相同。
这是怎么回事?!
您实际上是在尝试使用 MySQLi 的 multi_query,但是 MySQLi 不处理定界符。您应该将两个查询分开,这样您就不需要分隔符。
尝试:
<?php
$DBConnect = mysqli_connect("localhost", "root", "************");
mysqli_set_charset($DBConnect, "utf8");
mysqli_select_db($DBConnect, 'minecraft_xenforo');
$sql = "DROP TRIGGER IF EXISTS sign_chat";
$DBConnect->query($sql);
$sql = "
CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat
FOR EACH ROW BEGIN
IF (INSTR(NEW.message,"(") != 1) THEN
SET NEW.message = CONCAT("(MC) ", NEW.message);
END IF ;
END
";
$DBConnect->query($sql);
当我将它粘贴到相关数据库的 phpmyadmin 查询 window 中时,这个用于插入触发器的查询有效。但是当我尝试使用 mysqli_query 将它查询到数据库时,换行符似乎消失了导致它失败。
代码:
$TriggerQwry = 'Delimiter $$
DROP TRIGGER IF EXISTS sign_chat;
$$
CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat FOR EACH ROW BEGIN
IF (INSTR(NEW.message,"(") != 1) THEN
SET NEW.message = CONCAT("(MC) ", NEW.message);
END IF ;
END ;';
$DBConnect = mysqli_connect("localhost", "root", "************");
mysqli_set_charset($DBConnect, "utf8");
mysqli_select_db($DBConnect, 'minecraft_xenforo');
$QueryResult = mysqli_query($DBConnect, $TriggerQwry);
if($QueryResult)
{
$result = mysqli_affected_rows($DBConnect);
}
else
{
$result = 0;
echo "<p>Unable to execute the query.[".$TriggerQwry."]</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect) . "</p>";
}
echo "Result[$Result]";
mysqli_close($DBConnect);
输出:
Unable to execute the query.[Delimiter $$ DROP TRIGGER IF EXISTS sign_chat; $$ CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat FOR EACH ROW BEGIN IF (INSTR(NEW.message,"(") != 1) THEN SET NEW.message = CONCAT("(MC) ", NEW.message); END IF ; END ;]
Error code 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Delimiter $$ DROP TRIGGER IF EXISTS sign_chat; $$ CREATE ' at line 1
我还尝试在 TriggerQwry 字符串上使用全引号 "" 和 \n 以及 \n\r 和 \r\n,但结果都相同。
这是怎么回事?!
您实际上是在尝试使用 MySQLi 的 multi_query,但是 MySQLi 不处理定界符。您应该将两个查询分开,这样您就不需要分隔符。
尝试:
<?php
$DBConnect = mysqli_connect("localhost", "root", "************");
mysqli_set_charset($DBConnect, "utf8");
mysqli_select_db($DBConnect, 'minecraft_xenforo');
$sql = "DROP TRIGGER IF EXISTS sign_chat";
$DBConnect->query($sql);
$sql = "
CREATE TRIGGER sign_chat BEFORE INSERT ON dark_taigachat
FOR EACH ROW BEGIN
IF (INSTR(NEW.message,"(") != 1) THEN
SET NEW.message = CONCAT("(MC) ", NEW.message);
END IF ;
END
";
$DBConnect->query($sql);