1

I am trying to get each result of a query into one variable so that I can include it into my wp_mail message.

This is what I am using.

$result = $wpdb->get_results("SELECT * 
                               FROM $tableName 
                               WHERE userId = $user_ID 
                               AND status LIKE '1'"
                            );
foreach( $result as $key => $value)
{
    $string .= $value.',';
}

echo $string;
$email = $_POST['parentsEmail'];    
$child = $_POST['childsName'];
wp_mail( $email, "".$child." has finished", "".$child." has completed the following ".$string." ");

I have been searching and found another thread asking something similiar and this was suggested.

$string .= $value.',';

However when I try to do the same thing, I get

Catchable fatal error: Object of class stdClass could not be converted to string in

4
  • 1
    Value is an array right? Don't you need to explode the array or implode it? Commented Jan 10, 2014 at 14:36
  • 1
    what are you getting in $result? do print_r or var_dump Commented Jan 10, 2014 at 14:36
  • make sure you get one row Commented Jan 10, 2014 at 14:38
  • Yea I always end up with array... Commented Jan 10, 2014 at 14:53

2 Answers 2

2

You can use this instead using mysql

SELECT 
    CONCAT(col1,col2,col3) `value`
FROM $tableName 
WHERE userId = $user_ID AND status LIKE '1'

You can use this instead using mysql ok

Sign up to request clarification or add additional context in comments.

Comments

1

You can concat rows field in SQL statement with GROUP_CONCAT() function:

$sql = "
    SELECT GROUP_CONCAT(field_name) AS concat_alias
    FROM $tableName
    WHERE userId = $user_ID AND status LIKE '1'
";
echo $string = $wpdb->get_var($sql);

6 Comments

if you are using gropu_cancat, then you have to use group by.
@kumar_v: not in this case.
I end up with array? How do I get the contents of the array?
@user1949929: I have just updated the answer, where you fetch using get_var().
@user1949929: np, glad I can help. If you wish to use different separator, you can set that in GROUP_CONCAT() function like: GROUP_CONCAT(field_name SEPARATOR ' | ')...
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.