I'm not sure why you are getting errors but I can show you haw to defensively find and react to them.
This is your code
$rows = $wpdb->get_results($wpdb->prepare("SELECT `rul_value` FROM " . _ROLES_ . " WHERE `rul_type` = %s AND `rul_value` LIKE %s", 'role', '%intera%'));
foreach($rows as $row) {
$singleParts .= $row->rul_value . "\n";
}
Let's make some changes.
Do you have data inside $singleParts
already? If not, we can force it to be a clean empty var with $singleParts = '';
.
Like this:
$rows = $wpdb->get_results($wpdb->prepare("SELECT `rul_value` FROM " . _ROLES_ . " WHERE `rul_type` = %s AND `rul_value` LIKE %s", 'role', '%intera%'));
$singleParts = '';
foreach($rows as $row) {
$singleParts .= $row->rul_value . "\n";
}
Now we can cross that off the list of things going wrong. Next, you should inspect for an error. (Care of this answer)
if($wpdb->last_error !== ''){
echo 'Opps something went wrong: ';
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'>
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
<code>$query</code></p>
</div>";
}else{
# your code
}
Finally we can inspect the data you got back. We expected a string so we should make sure we got a string:
if(is_string($row->rul_value)){
# your code
}else{
echo "I was not expecting this. We got: <pre>";
print_r($row->rul_value);
echo "</pre>";
}
putting it al together we have:
$rows = $wpdb->get_results($wpdb->prepare("SELECT `rul_value` FROM " . _ROLES_ . " WHERE `rul_type` = %s AND `rul_value` LIKE %s", 'role', '%intera%'));
$singleParts = '';
if($wpdb->last_error !== ''){
echo 'Opps something went wrong: ';
$str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
print "<div id='error'>
<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
<code>$query</code></p>
</div>";
}else{
# no wpdb error
foreach($rows as $row) {
if(is_string($row->rul_value)){
# it is the string we were looking for
$singleParts .= $row->rul_value . "\n";
}else{
# not a string - what did we get?
echo "I was not expecting this. We got: <pre>";
print_r($row->rul_value);
echo "</pre>";
}
}
}
Just echoing out possibly senstive data is not a great idea in production but for your development, this will help you trace where the errors are coming from. I recommend always assuming your data is mangled and checking it before doing things with it. Code is dumb and will do exactly what it was told regardless of our intentions.
$singleParts
initialised before the loop? ( Is it or do you use it out of nowhere? ). Have you tried printing the results of$row
instead? Note that you might get more useful information out of functions such asvar_dump
or a PHP debugger$singleParts = '';
here, but you could also have done a$singleParts = implode( "\n", $rows );
and skipped the loop entirely