mysql – 在列名称之前登录SQL语句
作者:互联网
我在PHP文件中有一个INSERT语句,其中符号(@)出现在列名前面.
@ FIELD1,
@场2,
这是一个MySQL数据库.符号是什么意思?
编辑:
PHP脚本中没有SET @ field1:=’test’. PHP脚本读取csv并将数据放入表中.是否可以将其误用为评论功能?
<?php
$typo_db_username = 'xyz'; // Modified or inserted by TYPO3 Install Tool.
$typo_db_password = 'xyz'; // Modified or inserted by TYPO3 Install Tool.
// login
$_SESSION['host'] = "localhost";
$_SESSION['port'] = "3306";
$_SESSION['user'] = $typo_db_username;
$_SESSION['password'] = $typo_db_password;
$_SESSION['dbname'] = "database";
$cxn = mysqli_connect($_SESSION['host'], $_SESSION['user'], $_SESSION['password'], $_SESSION['dbname'], $_SESSION['port']) or die ("SQL Error:" . mysqli_connect_error() );
mysqli_query($cxn, "SET NAMES utf8");
$sqltrunc = "TRUNCATE TABLE tablename";
$resulttrunc = mysqli_query($cxn,$sqltrunc) or die ("Couldn’t execute query: ".mysqli_error($cxn));
$sql1 = "
LOAD DATA LOCAL
INFILE 'import.csv'
REPLACE
INTO TABLE tablename
FIELDS
TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '\"'
IGNORE 1 LINES
(
`normalField`,
@field1,
@field2,
`normalField2`,
@field3,
@field4
)";
$result1 = mysqli_query($cxn,$sql1) or die ("Couldn’t execute query: " . mysqli_error($cxn));
?>'
解:
最后,我发现了! @字段用作虚拟来错过csv文件中的列.见http://www.php-resource.de/forum/showthread/t-97082.html
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
解决方法:
@符号是SQL中的变量.
在MySQL中,它用于在查询的连续运行之间存储值,或用于在两个不同查询之间传输数据.
一个例子
在两个查询之间传输数据
SELECT @biggest:= MAX(field1) FROM atable;
SELECT * FROM bigger_table WHERE field1 > @biggest;
另一种用法是排名,MySQL没有本机支持.
存储连续运行查询的值
INSERT INTO table2
SELECT @rank := @rank + 1, table1.* FROM table1
JOIN( SELECT @rank := 0 ) AS init
ORDER BY number_of_users DESC
请注意,为了使其工作,必须修复查询中处理行的顺序,这很容易出错.
看到:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
mysql sorting and ranking statement
http://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/
UPDATE
此代码永远不会有效.
您之前刚刚打开了连接,并且没有设置@fields.
所以目前他们持有空值.
最重要的是,你不能使用@vars来表示字段名,你只能使用@vars来表示值.
$sql1 = "
LOAD DATA LOCAL INFILE 'import.csv'
REPLACE INTO TABLE tablename
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\"'
IGNORE 1 LINES
(`normalField`, @field1, @field2, `normalField2`, @field3, @field4)";
标签:at-command,sql,mysql,symbols 来源: https://codeday.me/bug/20191002/1840699.html