开发者

When using MySQL request in foreach, first select is good, but then not

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 :'.my开发者_JS百科sql_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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜