admin管理员组文章数量:1326304
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 badges1 Answer
Reset to default 0You 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.
本文标签: pluginsInvalid argument supplied for foreach()
版权声明:本文标题:plugins - Invalid argument supplied for foreach() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742207811a2433145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论