2

I'm extracting field content forma a simple table under WordPress. The table contains the fields as in the screenshot here below and I need to extract all the values in rul_value for the row in which the field rul_type is "role"

enter image description here

So, I've made this query

$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";
}

but when printing it with

echo $singleParts;

or with

print_r($singleParts);

I get the list but the first word is "Array". Always. Actually it's not any array ... just this word appears. Which is very annoying

enter image description here

Here the output with var_dump($singleParts)

enter image description here

How can I get rid of? I have not clue why it's occurring this. Where am I wrong?

4
  • where is $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 as var_dump or a PHP debugger
    – Tom J Nowell
    Commented Sep 30 at 15:55
  • hello @TomJNowell. $singleParts It's not initialized. However only in this case I get this problem. Not in any other cases at all. I update the post with var_dump() result in a moment Commented Sep 30 at 16:05
  • I wish you'd posted your solution instead of deleting! It's generally good practice to set the intial value with $singleParts = ''; here, but you could also have done a $singleParts = implode( "\n", $rows ); and skipped the loop entirely
    – Tom J Nowell
    Commented Sep 30 at 16:24
  • Undeleted. yes I tried to create the variable before the foreach and it worked. Still the fact I have not clue where that variable takes the word "Array". It shouldn't be. That's why I deleted the post. because it is ok a workaround on my case but not the root of the cause. Commented Sep 30 at 16:28

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.