在foreach中使用MySQL请求时,先select可以,然后不行
使用此代码:
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 '<p>No data to fetch</p>';
}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 '<br/>';
echo $value[0] . ' ';
if( in_array($value[0], $found_list) ){
echo "ENTRY EXISTS\n <br/>";
}else{
echo "ENTRY DOES NOT EXIST\n <br/>";
}
}
结果是:
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
With this code:
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';
}
}
For the first $value
it finds an entry, which is correct. For the next ones it doesn't, but it should. How can this be fixed?
Update code
With this code:
$found_list = array();
$fetch_list = array();
foreach($content as $value){
$fetch_list[] = "'" . mysql_real_escape_string($value[0]) . "'";
}
if( empty($fetch_list) ){
echo '<p>No data to fetch</p>';
}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 '<br/>';
echo $value[0] . ' ';
if( in_array($value[0], $found_list) ){
echo "ENTRY EXISTS\n <br/>";
}else{
echo "ENTRY DOES NOT EXIST\n <br/>";
}
}
And the result is :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由用几乎相同的查询淹没 MySQL 服务器。看一下
IN
表达式:我还建议你google一下SQL注入和XSS攻击。
编辑:以下是解决最新评论中所述问题的一些代码:
更新问题的答案:
PHP 比较运算符区分大小写:
您可以使用 strtolower() 在比较之前对值进行标准化。
There is no reason to flood the MySQL server with almost identical queries. Have a look at the
IN
expression:I also suggest you google for SQL Injection and XSS attacks.
Edit: Here's some code that solves the problem as described in latest comments:
Answer to updated question:
PHP comparison operators are case sensitive:
You can use strtolower() to normalize values before comparing.