mysql - PHP in_array function not working with mysqli_fetch_array -


if not use mysql, php in_array function works expected. in example, if statement execute , display the array contains keyword blue because in_array function able find keyword blue in array.

<?php $keyword = "blue"; $array = array("red", "green", "blue", "yellow");  if (in_array($keyword, $array)) {echo "the array contains keyword $keyword";} else {echo "the array not contain keyword $keyword";} ?> 

in_array fails same when selecting red, green, blue, yellow mysql. in example, if echo $array, red green blue yellow displayed in web browser, know blue in $array variable. however, else statement executes , displays the array not contain keyword blue.

<?php $keyword = "blue";  $con = new mysqli('domain','username','password','database'); $sql = "select * colors"; $sql_query = $con->query($sql);  while ($row = mysqli_fetch_array($sql_query)) {   $array = $row['colors'];    if (in_array($keyword, $array)) {echo "the array contains keyword $keyword";}   else {echo "the array not contain keyword $keyword";} } ?> 

if has tips or suggestions here, appreciate it.

in sql version record field named colors should exist.
field contains string (maybe comma-separated list, blue,red,...), not array.

so when execute in_array($keyword, $array) fire php error (but have not error_report set show them).

what must transform content got in colors field array.
in case evoked above:

$array = explode(',', $row['colors']); 

or whatever appropriate depending on colors field structure.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -