duanjiani6826 2016-05-19 16:05
浏览 46
已采纳

php中的错误 - foreach

The error I get is Warning: Invalid argument supplied for foreach() , I have read the relevant post for foreach case, but I still dont know how to fix it. Can I know what to cause this warning and the way to fix it ?

Here is my code,

mysql_connect("localhost","psm","psm") or die("could not connect");
mysql_select_db("planner") or die ("could not find db!");

$output = '';
//collect
if(isset($_POST['search']))
{
    $searchq =$_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    $query = mysql_query("select c.*,u.username from login_user u left join calendar c on c.user_id = u.user_id where u.matrix LIKE '{$searchq}'" ) or die ("could not search");
    $count = mysql_num_rows($query);
    if ($count == 0){ 
    $output = 'There are no search result!';
    } else {
    while($row = mysql_fetch_array($query)){
         $searchq = [];
         $searchq[] = $row; 
        }
}
}
    foreach($searchq as $u) {
?>
        <div class="well" align="left" width="65%" >
        <fieldset>
        <table>
            <tr><th>Matrix No:
            <?php echo $query['matrix']; ?>
            <?php foreach($query as $c){
                    if($c['user_id'] == $u['user_id']){
                        if($c['cate'] == 0){
                            echo "<p style='background: red;'>Course Name : ";
                        }else{
                            echo "<p>Title : ";
                        }
            ?>
                    <?php echo $c['title']; ?></p>
                    <p>Description :<?php echo $c['description'];?></p>
                    <p>Location : <?php echo $c['location'];?></p>
                    <p>Start Time : <?php echo date('Y-m-d',$c['starttime']);?></p>
                    <p>End Time : <?php if($c['endtime'] != 0)echo date('Y-m-d',$c['endtime']);?></p>
            <?php }}?>
        </th></tr>
        </table>
        </div>

<?php    
    }
?>
  • 写回答

1条回答 默认 最新

  • dshtze500055 2016-05-19 16:13
    关注

    Okay, you've got a lot of problems here.

    First, don't use mysql_ functions, as they've been deprecated since PHP 5.5, and removed in 7.0. Use mysqli_ functions instead, or PDO.

    Second, if you don't have anything in $_POST['search'], then $searchq is never initialized, and you have nothing for your foreach loop.

    Second and a half, if you do pass in a $_POST['search'], but no results are found, you have $searchq as a string, so when it hits the foreach loop, it's not something foreach can use. (Honestly, your variables shouldn't do double duty like that, where you have $searchq as a string in the preg_replace and then use it as an array if there are search results.)

    Third, when you use mysqli_query() (as you should) it requires two parameters. It's called using

    $results = mysqli_query($connection, $statement);
    

    Fourth, you're reinitializing $searchq in every pass through your while loop.

    So, you're possibly not actually getting any results, so your while loop might never do anything, and therefore, $searchq is not an array that can be looped through with foreach.

    What you need to do is initialize $searchq before your query, so you have an empty array, so that when you get down to your foreach, you at least have an empty array there.

    Basically, above your foreach, you should have

    if( !empty($searchq) && is_array($searchq))
    {
        foreach(...)
    }
    

    or move the foreach inside your else statement after the while loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?