使用此代码:
foreach ($content as $value) { $data=$value[0]; echo $data; $req="SELECT * FROM TABLE WHERE data='$data'"; $result=mysql_query($req) or die ('Erreur :'.mysql_error()); if (mysql_num_rows($result)){ echo ' ENTRY EXISTS'; } else { echo ' ENTRY DOES NOT EXIST'; } }
对于第一个,$value
它找到一个条目,这是正确的.对于下一个它没有,但它应该.怎么解决这个问题?
更新代码
使用此代码:
$found_list = array(); $fetch_list = array(); foreach($content as $value){ $fetch_list[] = "'" . mysql_real_escape_string($value[0]) . "'"; } if( empty($fetch_list) ){ echo 'No data to fetch
'; }else{ $sql = 'SELECT DISTINCT inst_name FROM INSTITUTS WHERE inst_name IN (' . implode(', ', $fetch_list) . ')'; $res = mysql_query($sql) or die ('Error: ' . mysql_error()); while( $row = mysql_fetch_assoc($res) ){ $found_list[] = $row['inst_name']; } var_dump($found_list); } foreach($content as $value){ echo '
'; echo $value[0] . ' '; if( in_array($value[0], $found_list) ){ echo "ENTRY EXISTS\n
"; }else{ echo "ENTRY DOES NOT EXIST\n
"; } }
结果是:
array(3) { [0]=> string(13) "AixEnProvence" [1]=> string(19) "AixEnProvenceAnnexe" [2]=> string(7) "Acheres" } acheres ENTRY DOES NOT EXIST AixEnProvence ENTRY EXISTS aixenprovenceannexe ENTRY DOES NOT EXIST instituttest ENTRY DOES NOT EXIST
Álvaro Gonzá.. 5
没有理由用几乎相同的查询来淹没MySQL服务器.看看IN
表达式:
SELECT foo, bar FROM table WHERE data IN ('a', 'b', 'c');
我还建议你谷歌搜索SQL注入和XSS攻击.
编辑:这里有一些代码解决了最新评论中描述的问题:
No data to fetch'; }else{ $sql = 'SELECT DISTINCT data FROM table WHERE data IN (' . implode(', ', $fetch_list) . ')'; $res = mysql_query($sql) or die ('Error: ' . mysql_error()); while( $row = mysql_fetch_assoc($res) ){ $found_list[] = $row['data']; } } foreach($content as $value){ echo $value[0] . ' '; if( in_array($value[0], $found_list) ){ echo "ENTRY EXISTS\n"; }else{ echo "ENTRY DOES NOT EXIST\n"; } } ?>
回答更新的问题:
PHP 比较运算符区分大小写:
您可以使用strtolower()在比较之前标准化值.
没有理由用几乎相同的查询来淹没MySQL服务器.看看IN
表达式:
SELECT foo, bar FROM table WHERE data IN ('a', 'b', 'c');
我还建议你谷歌搜索SQL注入和XSS攻击.
编辑:这里有一些代码解决了最新评论中描述的问题:
No data to fetch'; }else{ $sql = 'SELECT DISTINCT data FROM table WHERE data IN (' . implode(', ', $fetch_list) . ')'; $res = mysql_query($sql) or die ('Error: ' . mysql_error()); while( $row = mysql_fetch_assoc($res) ){ $found_list[] = $row['data']; } } foreach($content as $value){ echo $value[0] . ' '; if( in_array($value[0], $found_list) ){ echo "ENTRY EXISTS\n"; }else{ echo "ENTRY DOES NOT EXIST\n"; } } ?>
回答更新的问题:
PHP 比较运算符区分大小写:
您可以使用strtolower()在比较之前标准化值.