0

I have a form field and would like to check for each input field if it is already exist in database.

Below is my php code:

$sql_query = "SELECT * FROM test_table";
if($result_query = $connect->query($sql_query))
{
    $items = array("firstName", "lastName", "country", "phone");

    //$names variable contains stored values which i've declared at the beginning
    $names = array($firstName, $lastName, $country, $phone);

    foreach(array_combine($items, $names) as $item => $name)
    {
        $array_items = "";
        $check_query = "SELECT $item FROM test_table WHERE $item = '".$name."'";
        $result_check_query = $connect->query($check_query);
        if($row_query = $result_check_query->num_rows)
        {
         $array_items .= $item . ", ";
        }
        echo $array_items . "gettype()=> " . gettype($array_items);
        echo "<br>";
    }
    print_r ( "<br>" . $array_items); 
}

The results from echo $array_items is as below:

Results from $array_items

The problem is when i print/echo out $array_items outside foreach loop, it only gets phone

enter image description here

how can i get all the individual strings as one and assigned it to a variable in php?

thanks in advance.

if there're link to old post, please provide me the link. i do not know the term use to search for this kind of problem.

3
  • do you want array at the end as a final output (containating group string outputs in seperate indexes)? Commented Oct 13, 2017 at 6:00
  • that would be helpful too. yes please show me @AlivetoDie Commented Oct 13, 2017 at 6:03
  • you already have answer and it's selected also Commented Oct 13, 2017 at 6:07

1 Answer 1

1

Please try this code

$sql_query = "SELECT * FROM test_table";
if($result_query = $connect->query($sql_query))
{
    $items = array("firstName", "lastName", "country", "phone");

    //$names variable contains stored values which i've declared at the beginning
    $names = array($firstName, $lastName, $country, $phone);
    $array_items = "";
    foreach(array_combine($items, $names) as $item => $name)
    {

        $check_query = "SELECT $item FROM test_table WHERE $item = '".$name."'";
        $result_check_query = $connect->query($check_query);
        if($row_query = $result_check_query->num_rows)
        {
         $array_items .= $item . ", ";
        }
        echo $array_items . "gettype()=> " . gettype($array_items);
        echo "<br>";
    }
    print_r ( "<br>" . $array_items); 
}

The problem with your code is assignment of $array_items variable in foreach loop. Each time loop execute it set the value to blank ($array_items = "";)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my.. thank you very much. how dumb am i to put the initial variable in foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.