Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 像给定值一样从SQL数组中检索所有数据_Php_Mysql_Arrays_While Loop_Sql Like - Fatal编程技术网

Php 像给定值一样从SQL数组中检索所有数据

Php 像给定值一样从SQL数组中检索所有数据,php,mysql,arrays,while-loop,sql-like,Php,Mysql,Arrays,While Loop,Sql Like,我试图从一个数据库中检索所有的数据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()); w

我试图从一个数据库中检索所有的数据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='nice phone';
$construct=mysql\u query(“从详细信息中选择标记数组、名称、id
其中标记“\u数组,如$new\u字符串%”)
或者死亡(“死亡20:$construct
”.mysql_error()); 而($getThis=mysql\u fetch\u数组($construct)){ echo$getThis['id']。
; 回音条斜杠($getThis['name'])。
; }
它根本不起作用。 你能给我指一下正确的方向吗?
我真的很挣扎

您应该将$new\u字符串加引号

注意这是一种非常糟糕的做法,您应该始终转义传递给SQL的所有变量。您应该认真阅读SQL注入和其他安全问题

另外,如果您想在标记数组中的任何位置匹配$new_字符串(您很可能需要),您也需要在其前面添加美元符号。你可以在网站上阅读更多

因此,最后:

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

在将数据放入查询之前,应清理数据,如:

  $new_string = "blah...; DROP TABLE tag_array; #";
  $sql = mysql_real_escape_string($new_string);
  $sql = "SELECT tag_array, name, id FROM details WHERE tag_array LIKE %'$sql'%"

这是不够的,尽管它有助于防止SQL注入,考虑使用正则表达式来清理数据。如果您还不了解regexp,请查看此网站:。它帮了我大忙。

“它根本不起作用。”那么会发生什么呢?我加了单引号,很抱歉这是打字错误。它与数组中的所有单词不匹配。第一个字。有没有办法解决这个问题?@Konerak:谢谢。我知道这一点,但只是忘了逃避。我添加了单引号,很抱歉这是打字错误。它与数组中的所有单词不匹配。第一个字。有没有办法解决这个问题?我想说清楚。数组1为=(男孩、女孩、桌子、椅子)。要搜索的单词是表。它不回显数组1,因为它只与第一个单词进行比较。我不确定我是否正确理解您的意思,但如果您想在标记数组中的任何位置找到该单词,您需要在搜索单词的前面和后面添加%。现在它只搜索以您的word开头的标记数组。@enoyhs:谢谢您的修改。请添加有关您的问题的评论(向下投票)。