我试图将数组的SQL结果转换为字符串,以便在日期变量之间使用变量。然而,我不断收到“类stdClass的对象无法转换为字符串”错误。我尝试过内爆字符串,但一直出现错误。我对内爆的理解是,它将数组变量转换为字符串。
PHP代码(我根据一个查询返回一个较低的日期,该查询根据用户输入的单个日期动态构建日期):
<?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;
}
?>
查询返回我需要的正确日期。打印$low\\u日期时,我得到:
Array ( [0] => stdClass Object ( [low_date] => 2020-02-21 ) )
我试图在查询中使用$low\\u date和$high\\u date(未显示)来查看日期字段:
<?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>";
}
当我硬编码日期时,查询在PHPmyAdmin中按预期工作,并且在打印变量时可以看到返回的正确日期,因此我认为我的问题是需要将数组转换为字符串。
在$low\\u日期的初始注册下方,我尝试将其内爆:
$low_date_str = implode($low_date);
然后我每次都会收到错误。
我错过了什么?我对PHP有点陌生,所以我可能只是缺少一些简单的东西。
Update: Tom帮助我理解了我需要将数组的值(总是以单键数组的形式返回)传递给一个新字符串,但由于对PHP来说有点陌生,我不确定如何检索数组键的值。
$low_date_str = $low_date[0]->low_date;
$high_date_str = $high_date[0]->high_date;
检索键并将其传递给新变量以在SQL中使用时,我只需要这些代码。再次感谢你,汤姆!