1

I need to check the array values which contains more than 400 values which is existing in the MySQL table where i should get result for all the values which I am passing even if it not exist in the column.

for example:

cont table
----------
Column1                  
---------             

9xxxxxxxxx

91xxxxxxxx

92xxxxxxxx

my array contains 9xxxxxxxxx & 91xxxxxxxx. How can i iterate to get the column exist or not.

I tried with foreach and building query

$sql = 'SELECT cont WHERE num IN(';

foreach($jsonString as $val){ 

$sql = $sql . "'$val', ";

echo $val;

}

if(mysqli_query($con,$sql)){  
    echo "exist";
}else {
    echo "error";
}

Is there any efficient way other than the above which is faster in time?

5
  • what is this?if(mysqli_query($con,$sql)){ echo "exist"; }else { echo "error"; } Commented Nov 17, 2016 at 12:35
  • You will always have a trailing comma and space at the end of the sql statement. You would be better off imploding the array implode( ',', $jsonString ) Commented Nov 17, 2016 at 12:42
  • @Blinkydamo: yup you r right, Commented Nov 17, 2016 at 12:45
  • 1
    @devpro you too, have an up vote :) Commented Nov 17, 2016 at 12:46
  • did u checked? .. Commented Nov 17, 2016 at 13:55

1 Answer 1

4

First of all if cont is your table name than where is FROM keyword.

If cont is column name than where is TABLE name?

As per your question, you have 400+ ids, than why are you using loop here? you can simply use implode() with comma seperated values, like

<?php
$ids = implode(",",$jsonString);
$sql = "SELECT * FROM cont WHERE num IN ($ids)"; // assuming cont is table name
?>

In your loop you will get the "," in last iteration at the end this will break your query.

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

Comments

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.