代码之家  ›  专栏  ›  技术社区  ›  beginner

php:无法对每个循环进行迭代

  •  0
  • beginner  · 技术社区  · 12 年前

    我面临来自以下代码的以下错误:

    错误:

    Warning: Invalid argument supplied for foreach() 
    

    代码:

     //--------This is the code for jquery UI autocompleter-------------
    
    include "inc/connect.php";
    $skeyword = $_GET["term"];
    $req = "SELECT  p.prm_title ,a.am_name, c.cm_name ,l.lm_name ,b.bm_name,r.pm_name  "
        . "FROM product_master p,author_master a,category_master c,language_master l,binding_master b ,publisher_master r "
        . "WHERE p.prm_title LIKE '%$skeyword%' OR a.am_name LIKE '%$skeyword%'
           OR c.cm_name LIKE '%$skeyword%' OR l.lm_name LIKE '%$skeyword%' OR b.bm_name LIKE '%$skeyword%' OR r.pm_name LIKE '%$skeyword%'  
           GROUP BY p.prm_id LIMIT 10";
    
    $res = mysql_query($req);
    $ret = array();
    foreach (mysql_fetch_array($res) as $row) {
        foreach ($row as $val) //--Error: from this line, Why?
        {
            if (false !== stripos($val, $skeyword)) {
                $ret[] = $val;
                break;
            }
        }
    }
    
    $testme = json_encode($ret);
    echo $testme;
    

    上面的代码是为jquery自动完成器编写的,用于在多列字段中搜索,但它只返回匹配的列。

    请帮我解决这个问题。。

    5 回复  |  直到 12 年前
        1
  •  0
  •   srain    12 年前

    为什么?

    如果变量:

    • 不是 array
    • 未实现接口 Iterator

    它用于迭代器 foreach ,将引发此错误。


    如何修复

    mysql_fetch_array 返回与提取的行相对应的字符串数组,或者 FALSE 如果没有更多的行,那么使用 while 循环以获取结果:

    while ($row = mysql_fetch_array($res))
    {
        // if $row is false, the code will not hit here.
        foreach ($row as $val)
        {
            if (FALSE !== stripos($val, $skeyword))
            {
                $ret[] = $val;
                break;
            }
        }
    } 
    


    更新:

        while ($row = mysql_fetch_array($res))
        {
            // if $row is false, the code will not hit here.
            foreach ($row as $val)
            {
                if (FALSE !== stripos($val, $skeyword) && !in_array($val, $ret))
                {
                    $ret[] = $val;
                    break;  // what this break for?
                }
            }
        } 
    
        2
  •  0
  •   Pupil    12 年前

    请试试这个。 使用 while 而不是 foreach

    //--------This is the code for jquery UI autocompleter-------------
    
    include "inc/connect.php";
    $skeyword = $_GET["term"]; 
    $req = "SELECT  p.prm_title ,a.am_name, c.cm_name ,l.lm_name ,b.bm_name,r.pm_name  "
            . "FROM product_master p,author_master a,category_master c,language_master l,binding_master b ,publisher_master r "
            . "WHERE p.prm_title LIKE '%$skeyword%' OR a.am_name LIKE '%$skeyword%'
                          OR c.cm_name LIKE '%$skeyword%' OR l.lm_name LIKE '%$skeyword%' OR b.bm_name LIKE '%$skeyword%' OR r.pm_name LIKE '%$skeyword%'  
                          GROUP BY p.prm_id LIMIT 10";
    
        $res = mysql_query($req);
                $ret = array();
                while ($row = mysql_fetch_array($res))
                {
                    foreach ($row as $val)   //--Error: from this line, Why?
                    {
                        if (FALSE !== stripos($val, $skeyword))
                        {
                            $ret[] = $val;
                            break;
                        }
                    }
                } 
    
    $testme = json_encode($ret);
    echo $testme;
    
        3
  •  0
  •   djot    12 年前
    while ($row = mysql_fetch_array($res)) {
    
        4
  •  0
  •   Bas Wildeboer    12 年前

    再一次,但现在作为一个答案:

    将你的第一个前臂变成while循环。请参阅 mysql_fetch_array manual

        5
  •  0
  •   Armage    12 年前

    总是 验证“foreach”的参数是否为数组。 如果没有与您的条件匹配的行,并且在FALSE上的foreach不起作用,则您的SQL请求可能会返回FALSE:)

    所以在你的代码之后

    foreach (mysql_fetch_array($res) as $row)
    

    在尝试迭代$row之前,您必须验证它是一个数组,而不是false。

    if (is_array($row))
    

    或者您可以更明确地使用mysql_num_rows,它会告诉查询是否返回某些行:

    if (mysql_num_rows($res) > 0) 
    {
        $ret = array();
        foreach ( mysql_fetch_array($res) as $row)
        // ...
    }
    

    但是,正如djot和Amol在上面公开的那样,正确的解决方案是使用

    while ($row = mysql_fetch_array($res))
    

    确保如果$row为FALSE,那么您不会进入块,也不会尝试在FALSE上迭代。

    推荐文章