Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress $wpdb->get_results() query [closed]

Tags:

wordpress

I am trying to run a mysql_fetch_array via Wordpress. I found out the best way to do this is explained here: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

Here is my query below:

$sql = "SELECT * FROM wp_reminders WHERE reminder LIKE '$today'";
$result = $wpdb->get_results($sql) or die(mysql_error());

    foreach( $result as $results ) {

        echo $result->name;
    }

The above is not pulling in any results at all, even though the data does exist. Any ideas what I am doing wrong?

like image 660
danyo Avatar asked Feb 10 '13 17:02

danyo


2 Answers

the problem was the following:

echo $result->name;

should be:

echo $results->name;
like image 132
danyo Avatar answered Oct 03 '22 06:10

danyo


The 'foreach' loop and the initial var statement for 'result = $wpdb->...' should be results.

$sql = "SELECT * FROM wp_reminders WHERE reminder LIKE '$today'";
$results = $wpdb->get_results($sql);

    foreach( $results as $result ) {

        echo $result->name;

    }

The logic behind this is that you would be gathering all results from the get_results() function and then looping through them as such: (read it out loud - the logic is enforced)

foreach ( $ofTheMassiveList as $aSingleResult ) {

        echo $aSingleResult->name;

}
like image 34
jharrell Avatar answered Oct 03 '22 07:10

jharrell