最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Invalid argument supplied for foreach()

programmeradmin0浏览0评论

I try ti get some value from a request and display it in a loop. I ve already use this system in an another function and that it worked but in this not. I get the message : Warning: Invalid argument supplied for foreach()

 function mic_services_shortcode($atts = [], $content = '') {
        global $post;
        $id = $post->post_name;
        $id = explode("-s", $id);
        global $wpdb;
        $prepare = $wpdb->prepare('SELECT DISTINCT wp_mic_keyword.keyword as keyword, wp_mic_keyword.image as image, wp_mic_keyword.texte as texte'. '  FROM wp_mic_keyword INNER JOIN wp_mic_rel_key_ser ON id_keyword where id_service=%d;', $id);
        $results = $wpdb->get_results($prepare);
        ob_start();
        echo '<div style="border: 1px solid black;>';
        foreach ($results as $key) { 
           echo 
           '<div>';      
            echo $key->keyword.'<br>';
            echo $key->image ."<br>";
            echo
            $key->texte;        
            echo '</div>';
        }
        echo '</div>';
        $html_form = ob_get_clean();
        return $html_form;
    }

I ve tested the request in SQL and it work fine.

I try ti get some value from a request and display it in a loop. I ve already use this system in an another function and that it worked but in this not. I get the message : Warning: Invalid argument supplied for foreach()

 function mic_services_shortcode($atts = [], $content = '') {
        global $post;
        $id = $post->post_name;
        $id = explode("-s", $id);
        global $wpdb;
        $prepare = $wpdb->prepare('SELECT DISTINCT wp_mic_keyword.keyword as keyword, wp_mic_keyword.image as image, wp_mic_keyword.texte as texte'. '  FROM wp_mic_keyword INNER JOIN wp_mic_rel_key_ser ON id_keyword where id_service=%d;', $id);
        $results = $wpdb->get_results($prepare);
        ob_start();
        echo '<div style="border: 1px solid black;>';
        foreach ($results as $key) { 
           echo 
           '<div>';      
            echo $key->keyword.'<br>';
            echo $key->image ."<br>";
            echo
            $key->texte;        
            echo '</div>';
        }
        echo '</div>';
        $html_form = ob_get_clean();
        return $html_form;
    }

I ve tested the request in SQL and it work fine.

Share Improve this question asked Aug 6, 2020 at 10:13 Zed93Zed93 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You will get this error if foreach() is not an array. So your first step should be to debug this by inspecting the contents of $results to confirm that the results are what you expect. If it's not then it might give you clues to why. This is debugging 101.

If you do that you'll see that you're not getting results. If you're not getting results then the next step is to find out why. The way to do that is to check what you actually queried. You can do this by inspecting what the value of $prepare is.

If you do that then you'll see that the SQL query does not match what you expect. In all likelihood the value for id_service= is missing, or incorrect.

Please note that absolutely none of these steps so far have required any WordPress development knowledge, special debugging tools or IDEs, or even any PHP knowledge beyond the very basics. Far more important than learning PHP or WordPress APIs is teaching yourself to follow these very very basic debugging steps.

So, if you followed that steps you'll see that the id_service= part of the query has a missing or incorrect value. What would be the cause of this? You're using $wpdb->prepare() to insert $id. But what is $id? On this line you've used explode() to create it:

$id = explode("-s", $id);

And what does explode() do? If you simply find the explode() article in the PHP manual you'll see that it:

Returns an array of strings...

So, $id is an array of strings.

And what does $wpdb->prepare() do? It safely substitutes placeholders with variables in a string of SQL. As documented:

The following placeholders can be used in the query string: %d (integer) %f (float) %s (string)

But in your code you've used %d, which is used to substitute an integer, but as noted above, $id is not an integer. It's an array of strings. So that's the cause if your problem: $id is not the right value for what you're trying to do.

That's as far as I can get without knowing all the bits and pieces of what you're trying to do. But if you follow the debugging steps I've outlined above, you should be able to figure out where you've gone wrong.

发布评论

评论列表(0)

  1. 暂无评论