admin管理员组

文章数量:1391925

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

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!

Share Improve this question edited Feb 23, 2020 at 22:38 dgibbons82 asked Feb 23, 2020 at 16:02 dgibbons82dgibbons82 33 bronze badges 1
  • This looks like a generic PHP question unrelated to WP other than context, this might be better off on Stack Overflow – Tom J Nowell Commented Feb 23, 2020 at 16:39
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: phpConverting Array to String Issue