可以';来自mysql';s数组


Can't fint specific element from mysql's array

所以我有一个小问题。我想把mysql中的数据保存到一个数组中,然后在该数组中搜索一个特定的单词,并检索是否找到,我已经尝试了很多方法,但仍然没有成功。它总是检索"未找到单词"。我也尝试过array_search和for循环。我错过了什么?

<?php
include("conn.php");
$keyword = "universidade";
$row = array();
$result = mysql_query("SELECT keywords FROM beta_universidades");
while ($data = mysql_fetch_assoc($result)) {
    $row[] = $data;
}
echo "<br><br>";
$chave = in_array($keyword, $row);
if ($chave !== false) {
    echo "word found";
} else {
    echo "word not found";
}

在您的案例中,$row是一个数组,因此您应该尝试以下操作:

foreach ($row as $myKeywords) {
   $chave = in_array($keyword, $myKeywords['keywords']);
   if ($chave !== false) {
      echo "word found";
      break;
   }
}

您忘记在$data数组中使用键'keywords'。。

while($data = mysql_fetch_assoc($result))
{
    $row[] = $data['keywords']; 
}