Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:35:39+00:00 2026-05-22T01:35:39+00:00

With this code: foreach ($content as $value) { $data=$value[0]; echo $data; $req=SELECT * FROM

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:35:39+00:00Added an answer on May 22, 2026 at 1:35 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
When I run this code: foreach($xml->movie as $movie) { if(isset($movie->photos)) { foreach ($movie->photos as
I'm currently adding some new extended classes to this code: foreach (BaseType b in
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
I have the following code: foreach (var control in this.Controls) { } I want
With doctrine if you execute this code $columns = $accountTable->getColumns(); foreach ($columns as $column)
Do you consider this a code smell? foreach((array)$foo as $bar) { $bar->doStuff(); } Should
I have code that I want to look like this: List<Type> Os; ... foreach
I have some code, int count = 0; list.ForEach(i => i.SomeFunction(count++)); This seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.