简体   繁体   English

使用PHP为MySQL重命名非法的列名

[英]Rename illegal column names with PHP for mySQL

I have a column named "read" in my table. 我的表中有一列“读取”。 When I try to perform operations on it, PHP tells me there's an error surround 'read'. 当我尝试对其执行操作时,PHP告诉我'read'周围存在错误。 From previous experience with this kind of errors, I have determined that the column name 'read' is illegal. 根据以前对此类错误的经验,我确定列名“ read”是非法的。 The problem is that I can't rename the column with PHP. 问题是我无法使用PHP重命名该列。 Every time I try, I get an error (the same error, actually). 每次尝试时,我都会收到一个错误(实际上是相同的错误)。 When I go in PHPMyAdmin, and change it there, it works perfectly afterwards. 当我进入PHPMyAdmin并在那里进行更改时,此后它可以完美运行。

Any ideas on how I can change the column name with PHP? 关于如何使用PHP更改列名的任何想法?

Use backticks around the table name, like this: 在表名周围使用反引号,如下所示:

select * from `read`

It tells MySQL that you are referring to a name and not a keyword. 它告诉MySQL您是指名称而不是关键字。

When you say "rename", do you mean you're trying to modify the table definition itself, like with ALTER TABLE ? 当您说“重命名”时,是否意味着要像ALTER TABLE一样修改表定义本身? If so, there shouldn't be a difference between your PHP code and PHPMyAdmin (since the latter is itself just PHP code). 如果是这样,您的PHP代码和PHPMyAdmin之间应该没有区别(因为后者本身就是PHP代码)。 The only problem you could run into is if you're logging in as a different database user in your PHP code than you are in PHPMyAdmin. 您可能遇到的唯一问题是,如果您以与PHPMyAdmin中不同的数据库用户身份在PHP代码中登录。 You need to make sure the user has ALTER privileges on that table. 您需要确保用户对该表具有ALTER特权

If you're only talking about running normal queries on the table, and just want to stop the syntax error from occurring, then you can just wrap your field and/or table names in backticks - ie ` usually on the same key as the tilde ~ on the number bar - and MySQL will be able to distinguish between the field name and the reserved word. 如果您只想在表上运行普通查询,并且只想停止语法错误的发生,那么您可以将字段和/或表名包装在反引号中-即`通常与代字号在同一个键上在数字栏上-MySQL将能够区分字段名称和保留字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM