PHP + SQL 查询:选择枚举值

发布于 2024-12-06 00:31:07 字数 661 浏览 0 评论 0原文

我遇到了一个奇怪的情况,PHP 执行 SQL 查询,也许有人可以解释一下:

我有一个查询说:

"SELECT COLUMN_TYPE 
 FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE TABLE_NAME = $table 
 AND COLUMN_NAME = $column"

查询应该返回一个字符串:enum('A',' B'[,'C'...]) 执行时(就像从命令行运行查询时一样)。$table$column传递给进行查询的函数,在我的测试用例中都是正确的 - 两者分别作为表和列存在,在本例中,让我们设置上面的 SQL 语句返回的 $table = 'profile'$column = 'gender'。以下错误:

Unknown column 'profile' in 'where clause' in /registry/mysqldb.class.php on line 31

第 31 行是一个缓存函数,用于在需要时缓存查询,或者在查询失败时触发错误。在发出请求时,我会连接到正确的数据库

。这:为什么是“个人资料”被解释为一列并导致错误?

I'm running into a weird situation with a PHP execution of a SQL query that maybe someone can shed a little light on:

I have a query that says:

"SELECT COLUMN_TYPE 
 FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE TABLE_NAME = $table 
 AND COLUMN_NAME = $column"

The query should return a string: enum('A','B'[,'C'...]) when executed (as it does when running the query from the command line. $table and $column are passed to the function making the query, and in my test case are proper – both exist as a table and column respectively. In this case, let's set $table = 'profile' and $column = 'gender', to which the above SQL statement is returning the following error:

Unknown column 'profile' in 'where clause' in /registry/mysqldb.class.php on line 31

Line 31 is a caching function to cache queries until required, or triggering an error when the query fails. This works fine in every other case. I'm connected to the correct DB when the request is made.

My question is this: Why is 'profile' being interpreted as a column and causing the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2024-12-13 00:31:07

也许是因为您缺少变量周围的引号。尝试:

"SELECT COLUMN_TYPE
 FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE TABLE_NAME = '$table' 
   AND COLUMN_NAME = '$column'"

Maybe it's because you are missing quotes around the variables. Try:

"SELECT COLUMN_TYPE
 FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE TABLE_NAME = '$table' 
   AND COLUMN_NAME = '$column'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文