gpt4 book ai didi

php - 从 SQL 数组中检索所有数据(类似于给定值)

转载 作者:行者123 更新时间:2023-11-29 14:49:48 27 4
gpt4 key购买 nike

我正在尝试从数据库中检索所有数据ID,其中它们的标签(数组)就像给定值。这就是我到目前为止所做的...

$new_string = 'nice phone';
$construct = mysql_query("SELECT tag_array, name, id FROM details
WHERE tag_array LIKE $new_string%")
or die("<p>died 20: $construct<br>" . mysql_error());

while($getThis = mysql_fetch_array($construct)){
echo $getThis['id'].'<br />';
echo stripslashes($getThis['name']).'<br />';
}

它完全不起作用。你能指出我正确的方向吗?我真的好难受!!

最佳答案

您应该将 $new_string 放在引号中。

注意这是非常糟糕的做法,您应该始终转义传递给 SQL 的所有变量。您确实应该阅读有关 SQL 注入(inject)和其他安全问题的内容。

此外,如果您想在 tag_array 中的任何位置匹配 $new_string (您最有可能想要的),您也需要在其前面添加美元符号。您可以在 MySQL reference manual 阅读更多内容.

所以最后:

"SELECT tag_array, name, id FROM details WHERE tag_array LIKE '%" . mysql_real_escape_string($new_string) . "%'"

关于php - 从 SQL 数组中检索所有数据(类似于给定值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014605/

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