lhy lhy - 3 years ago 89
MySQL Question

Echo array from sql in php program

I would like to get all the

post IDs
from
yesterday
on wordpress, the SQL query works well when I tried it on
phpMyAdmin
, it returns all
IDs
from yesterday. However when I tried to echo the
IDs
out of the array, the values in the array is '
Array
' instead of the
ID
, which should be numbers. I wonder why did the
IDs
in the array all turned into the word 'Array'. This is the code I wrote:

$yesterday = strtotime("-1 days");
$day = (int)date("d", $yesterday);
$month = (int)date("n", $yesterday);
$year = (int)date("Y", $yesterday);

$sql = "
SELECT ID
FROM wp_posts
WHERE 1 = 1
AND DAY(post_date) = $day
AND MONTH(post_date) = $month
AND YEAR(post_date) = $year
";

global $wpdb;
$results = $wpdb->get_results($sql, 'ARRAY_N');

foreach ($results as $id) {
echo '<br>' . $id;
}


Update:
I changed the type of output to
ARRAY_A
and added the
var_dump()
before the
foreach
loop to see what's inside the array, and this is what I got:

output

Answer Source

use ARRAY_A to get associative array instead of ARRAY_N which returns numerically indexed multidimensional array .. $results in your code is multidimensional array .. use var_dump() instead on echo to view variable contents ( for testing )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download