在foreach中使用MySQL请求时,先select可以,然后不行

发布于 2024-11-06 10:54:56 字数 1809 浏览 0 评论 0原文

使用此代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2024-11-13 10:54:56

没有理由用几乎相同的查询淹没 MySQL 服务器。看一下 IN 表达式

SELECT foo, bar
FROM table
WHERE data IN ('a', 'b', 'c');

我还建议你google一下SQL注入和XSS攻击。

编辑:以下是解决最新评论中所述问题的一些代码:

<?php

// $content = ...
$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 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 比较运算符区分大小写:

<?php
var_dump('Acheres'=='acheres'); // bool(false)
?>

您可以使用 strtolower() 在比较之前对值进行标准化。

There is no reason to flood the MySQL server with almost identical queries. Have a look at the IN expression:

SELECT foo, bar
FROM table
WHERE data IN ('a', 'b', 'c');

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:

<?php

// $content = ...
$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 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";
    }
}

?>

Answer to updated question:

PHP comparison operators are case sensitive:

<?php
var_dump('Acheres'=='acheres'); // bool(false)
?>

You can use strtolower() to normalize values before comparing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文