PHP + SQL 查询:选择枚举值
我遇到了一个奇怪的情况,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是因为您缺少变量周围的引号。尝试:
Maybe it's because you are missing quotes around the variables. Try: