gpt4 book ai didi

php - WHERE IN 语句返回 'where clause' 中的未知列

转载 作者:行者123 更新时间:2023-11-30 00:38:34 25 4
gpt4 key购买 nike

这些行工作得很好并且给了我正确的输出:

$query = "SELECT * FROM `scores` WHERE id IN ('tim', 'scrat')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

但是,如果我想将 ('tim', 'scrat') 替换为我首先使用从另一列获取的信息创建的某个变量:

$query = "SELECT friendsids FROM `scores` WHERE id='tom'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
$friendsids = ($row[0]);
echo ($friendsids); // this correctly logs: tim,scrat

// now the same as above, but using $friendsids:
$query = "SELECT * FROM `scores` WHERE id IN ($friendsids)";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

我不断收到错误

Query failed: Unknown column 'tim' in 'where clause'

所以看起来 WHERE 操作确实将 $friendsids 分成了几部分,但不知何故无法正确识别它们?

不确定它是否相关,我的表格如下所示:

CREATE TABLE `scores` (
`id` VARCHAR(20) PRIMARY KEY,
`score` INT(10) UNSIGNED NOT NULL DEFAULT ‘0’,
`friendsids` VARCHAR(1000)
)
TYPE=MyISAM;

最佳答案

您可以简单地使用由第一个查询组成的查询,而不是两次访问数据库并在所描述的问题中运行:

SELECT 
*
FROM
`scores`
WHERE
id IN (
SELECT
friendsids
FROM
`scores`
WHERE
id='tom')

否则您需要在第一个结果中添加引号,但我认为这可以节省时间和精力。

关于php - WHERE IN 语句返回 'where clause' 中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com