为什么我得到“为foreach提供的无效参数”?

问题描述:

I am creating a search within my database and when I used the foreach construct for my results to be echoed I'm getting "invalid argument being supplied for foreach..". What I don't understand is why this error is coming up because the foreach containing my errors works fine.

if (empty($errors)){
    $results = search_results($keywords);
    $results_num = count($results);

    foreach ($results as $result){
        echo '<p> <strong>', $result['TITLE'], '</strong> </p>';
    }
} else {
    foreach($errors as $error){
        echo $error, '</br>';
    }
}

The part of the search_results function that's being focused on is this

$results = "SELECT TITLE FROM occupationalinfo WHERE $where"; 
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0;

if ($results_num === 0){
    return false;
}else{
    while ($results_row = mysql_fetch_assoc($results)) {
        $returned_results[] = array(
            'title' => $results_row['TITLE']
        );      
    }
}

I am still new to programming so I understand if there is something I may have missed or just not quite get. I would greatly appreciate any tips or constructive criticism.

我在我的数据库中创建一个搜索,当我使用foreach结构为我的结果回应我是 得到“为foreach提供的无效参数......”。 我不明白为什么会出现这个错误,因为包含我的错误的foreach工作正常。 p>

  if(empty($ errors)){
 $ results =  search_results($ keywords); 
 $ results_num = count($ results); 
 
 foreach($ results as $ result){
 echo'&lt; p&gt;  &lt; strong&gt;',$ result ['TITLE'],'&lt; / strong&gt;  &lt; / p&gt;'; 
} 
}其他{
 foreach($ errors as $ error){
 echo $ error,'&lt; / br&gt;'; 
} 
} 
 
   code>  pre> 
 
 

正在关注的search_results函数部分是这个 p>

  $ results =“SELECT TITLE FROM occupationalinfo WHERE $ where  “;  
 $ results_num =($ results = mysql_query($ results))?  mysql_num_rows($ results):0; 
 
if($ results_num === 0){
 return false; 
} else {
 while($ results_row = mysql_fetch_assoc($ results)){
 $ returned_results [  ] = array(
'title'=&gt; $ results_row ['TITLE'] 
);  
} 
} 
  code>  pre> 
 
 

我仍然是编程的新手,所以我理解是否有一些我可能错过或者不太了解的东西。 我非常感谢任何提示或建设性的批评。 p> div>

if ($results_num === 0){
    return false;
}

I'd bet this is executing, so it's returning false. You cannot apply foreach to false. Perhaps you should return array(), or just check for false:

$results = search_results($keywords);
if ($results !== false) {
    $results_num = count($results);

    foreach ($results as $result){
        echo '<p> <strong>',$result['TITLE'],'</strong> </p>';
    }
}

Try php function is_array().

if (empty($errors)){
           $results = search_results($keywords);
            if(is_array($results)){
               foreach ($results as $result){
                 echo '<p> <strong>'. $result['TITLE'] .'</strong></p>';
               }
            } else {
                echo "<p> <strong>empty</strong></p>";
            }
    } 

or

 if (empty($errors)){
        $results = search_results($keywords);
        if(is_array($results)){
             $results_num = count($results);
             foreach ($results as $result){
               echo '<p> <strong>'. $result['TITLE']. '</strong> </p>';
             }
        }        
    } else {
        foreach($errors as $error){
            echo $error, '</br>';
        }
    }