Match and Find all values in an array from mysqli database

1916 views mysql
5

I have an array with values like

$slugs= array('abraham_lincoln','alan_watts','albert_camus','anne_frank','a_p_j_abdul_kalam','aristotle','arnold_schwarzenegger','audrey_hepburn','ayn_rand','barack_obama','benjamin_franklin','bill_gates','bob_dylan','bob_ross','brene_brown','bruce_lee','bryson_tiller','carl_sagan','chanakya','charles_bukowski','charlie_chaplin');

I want to check if the same slug exist in the database or not.

$selectslug="SELECT `slug` FROM `authors_list`";
$done=mysqli_query($con, $selectslug )or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);

single slug comes from

foreach($slugs as  $slug)
{ echo $slug; }

and single slug from DB comes as

foreach($done as  $data)
{ echo $data;}

So How can I use the code below

if (in_array($slug, $data))
{
      echo $slug." Found<br />";
      }
    else
      {
      echo $slug." Match not found<br />";
}   

without repeating it number of times of $slug or $data?

answered question

Why do you use in_array ? You have a string $slug and another string $data, you can compare them with just == !

Both data $slug and $data are in different foreach and so I have to use foreach in second foreach which makes the checking process of same variable to the number of values I have in array. Like if my $slugs array has 20 values it checks $data for 20 times before going to the next one. How can I solve it? Even if I use == ??

1 Answer

12

as $slug and $data are strings, so you can compare them as string with '===' or '==!' :

if ($slug === $data)
{
  echo $slug." Found<br />";
}
else
{
  echo $slug." Match not found<br />";
}   

posted this

Have an answer?

JD

Please login first before posting an answer.