2013-02-10 8 views
5

Próbuję uruchomić mysql_fetch_array przez Wordpress. I okazało się, że najlepszym sposobem na to jest wyjaśnione tutaj: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

Oto moje zapytanie poniżej:

$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; 
    } 

Powyższe nie ciągnie w ogóle żadnych rezultatów, mimo że dane nie istnieją. Jakieś pomysły, co robię źle?

+0

Co to jest '$ today'? – Rob

+0

Nie jesteś pewien, czym jest '$ today', ale możesz nie mieć znaku wieloznacznego'% 'w twoim' LIKE' clausule? – jOpacic

+0

$ today = date ("m/d/Y"); – danyo

Odpowiedz

13

problem był następujący:

echo $result->name; 

powinno być:

echo $results->name; 
+11

Albo lepiej, obie nazwy zmiennych w instrukcji foreach powinny zostać odwrócone. –

4

The '' foreach pętli i początkowej instrukcji var dla 'result = $ wpdb -> ...' powinno być rezultatem.

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

    foreach($results as $result) { 

     echo $result->name; 

    } 

Logika jest to, że można byłoby gromadzenie wszystkich wyników z get_results() i następnie zapętlenie przez nich jako takie: (czytaj na głos - logika jest wymuszane)

foreach ($ofTheMassiveList as $aSingleResult) { 

     echo $aSingleResult->name; 

}