0

I'm attempting to convert the SQL results of an array into a string so I can use the variables as BETWEEN date variables. However, I keep getting "Object of class stdClass could not be converted to string" error. I've tried imploding the string but I keep getting the error. My understanding of implode was that it will convert the array variable into a string.

PHP Code (I am returning a low date based off of a query that dynamically builds dates from a single date entered by a user):

 <?php
 global $wpdb;
$low_date = $wpdb->get_results("SELECT low_date FROM (SELECT low_date FROM (SELECT @row:=0) temp, (select * from (select adddate((SELECT main.date FROM (SELECT DATE_FORMAT((CONCAT(starting_deposit_dt_yy, '-', starting_deposit_dt_mm, '-', starting_deposit_dt_dd)), '%Y-%m-%d') AS `date`  FROM income WHERE user_id = $user_id) AS main) ,t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) low_date from (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v where low_date between (SELECT DATE_FORMAT((CONCAT(starting_deposit_dt_yy, '-', starting_deposit_dt_mm, '-', starting_deposit_dt_dd)), '%Y-%m-%d') FROM income where user_id = $user_id) and DATE_ADD(DATE(SYSDATE()), INTERVAL 365 DAY)) d WHERE (@row:=@row + 1) % 14 = 1 ORDER BY low_date ASC) s WHERE low_date <= DATE(SYSDATE()) ORDER BY low_date DESC LIMIT 1");
foreach($low_date as $row) {
    echo $row->low_date;
}

?>

The query returns the correct date that I need. When I print_r the $low_date, I get:

Array ( [0] => stdClass Object ( [low_date] => 2020-02-21 ) )

I'm trying to use the $low_date and $high_date (not shown) in a query to look between date fields:

<?php
 $table_name = "accounts";
 $results = $wpdb->get_results ( "SELECT * FROM (
SELECT account_name, current_balance, minimum_due, last_pmt_amt, DATE_FORMAT((CONCAT(next_pmt_dt_yy, '-', next_pmt_dt_mm, '-', due_day)), '%Y-%m-%d') AS next_pmt_dt, autopay, status, account_type, url FROM accounts) vwd
WHERE user_id = $user_id AND vwd.next_pmt_dt BETWEEN '$low_date' AND '$high_date' ORDER BY next_pmt_dt ASC" ); // Query to fetch data from database table and storing in $results
 if(!empty($results))   // Checking if $results have some values or not
 {
    echo "<table width='100%' border='1'>";   // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
    echo  '<tr>';  //This prevents duplicate rows for header records since it's above the loop
    echo '<th>Account</th>';
    echo '<th>Balances</th>';
    echo '<th>Min Due</th>';
    echo '<th>Last Payment Amount</th>';
    echo '<th>Next Payment Date</th>';
    echo '<th>Autopay</th>';
    echo '<th>Status</th>';
    echo '<th>Type</th>';
    echo '<th>URL</th>';
    echo '<th>Action</th>';
    echo '</tr>';
    echo "<tbody>";      

    foreach($results as $row){   
  //  $user_id = $row->user_id;               //putting the user_id field value in variable to use it later in update query
      echo '<td>' . $row->account_name.'</td>';
      echo '<td>' . $row->current_balance.'</td>';
      echo '<td>' . $row->minimum_due.'</td>';
      echo '<td>' . $row->last_pmt_amt.'</td>';
      echo '<td>' . $row->next_pmt_dt.'</td>';
      echo '<td>' . $row->autopay.'</td>';
      echo '<td>' . $row->status.'</td>';
      echo '<td>' . $row->account_type.'</td>';
      echo '<td>' . "<a target=\"_blank\" href=" .  $row->url. ">LINK</a>".'</td>';
      echo '<td>' . "<a target=\"_blank\" href=" .  $row->url. ">UPDATE</a>".'</td>';
      echo '</tr>';
    }
        echo "</tbody>";
    echo "</table>"; 
 }

Query works as expected in PHPmyAdmin when I hardcode the dates, and I can see the proper dates being returned when I print the variable, so I think my problem is that I need to convert the array to a string.

Below the initial registration of the $low_date, I attempt to implode it:

$low_date_str = implode($low_date);

And then I receive the error every time.

What am I missing? I'm a bit new to PHP so maybe I'm just missing something simple.

Update: Tom helped me understand that I needed to pass the value of the array (which always returns as a single key array) to a new string, but I wasn't sure how to retrieve the value of an array key due to being a bit new to PHP.

$low_date_str = $low_date[0]->low_date;
$high_date_str = $high_date[0]->high_date;

This code was all I needed to retrieve the key and pass it to a new variable to use in my SQL. Thanks again, Tom!

  • This looks like a generic PHP question unrelated to WP other than context, this might be better off on Stack Overflow – Tom J Nowell Feb 23 at 16:39
0

The problem here is that implode will take an array of strings, and join them together using a glue parameter, such as commas.

But by your own admission, $low_date is not an array of strings. It's an array of objects! Objects are not strings. Here each row is an object, where you have only a single parameter because in your query you asked for a single named option.

So you have several generic PHP methods to get the date out:

  • You could loop over the rows, and add the dates to a second array which you then use in implode
  • You could walk over the array using something like array_map to replace each item in the array with the date, classic functional programming
  • You could loop over and echo each date and wrap it in a <time> tag ( as you should be doing for semantic reasons anyway ), then you could add the commas using CSS

However, since this is WPSE, and to keep this on-topic you need also have 2 other options:

  • Use wp_list_pluck to create a new array by plucking out just the date field
  • Ask get_results to return a numbered or associative array instead using the second parameter it accepts, as documented on the official developer hub
|improve this answer|||||
  • I think I'm still a little confused. BTW, thanks for your excellent explanation. This is just due to my lack of PHP knowledge. If $low_date echo's as this: Array ( [0] => stdClass Object ( [low_date] => 2020-02-21 ) ) How can I extract the low_date value? I've tried implode and json_extract and json extract was as close to I came for extracting the value. I'm thinking there's probably an easier way to just extracting the value of the 0 key from an array that I'm missing. – dgibbons82 Feb 23 at 21:19
  • Your code already has examples of extracting the data, but stdClass just means it's an object, and you access stuff inside objects using ->, it's basic fundamental PHP. If this doesn't make sense to you then you need to go and look up the syntax for objects on php.net. If you don't learn that then the only answers you will be able to use are copy paste, which won't get you very far. $low_date is an array. That array contains objects. Those objects contain a low_date variable that has your value – Tom J Nowell Feb 23 at 21:50
  • Also, the snippet I would give you for how to access it would be identical to the very first block of code in your question. You already have that piece of knowledge, but it requires knowledge about PHP basics to understand. This isn't a WordPress problem, it's a PHP problem. WordPress knowledge and expertise won't be able to solve this for you – Tom J Nowell Feb 23 at 21:51
  • Thanks so much for your time, Tom. I appreciate it. I'll mark this as the answer and then learn some of the fundamentals. Have a nice weekend. – dgibbons82 Feb 23 at 21:58
  • 1
    I also just wanted to add that your first comment was what I was missing. It led me to finding out how to echo the contents of an array. Thank you! – dgibbons82 Feb 23 at 22:06

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