SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trouble with wordpress $wpdb global...

    For some reason, the $wpdb global isn't working here and I would like some opinions... here's my code...

    PHP Code:
    global $wpdb;

    $s $_GET['s'];

    $table_name $wpdb->prefix."event";

    $sql "SELECT * FROM $table_name ORDER BY $s ASC";
    echo 
    $sql;

    if(
    $results $wpdb->get_results($sql)) {
            echo 
    "<table class='getEvents'>
            <tr>
                <th>Event ID</th>
                <th>Event Name</th>
                <th>Date Added</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>"
    ;

            foreach(
    $results as $result) {
                echo 
    "<tr>";
                echo 
    "<td class='eventID'>" $result->eid"</td>";
                echo 
    "<td class='eventName'>" $result->event_name"</td>";
                echo 
    "<td class='TimeDate'>"$result->date_added ."</td>";
                echo 
    "<td class='admin_added'>" $result->admin_added"</td>";
                echo 
    "<td class='edit'><a>Edit</a></td>";
                echo 
    "<td class='delete'>Delete</td>";
                echo 
    "</tr>";
            }
            echo 
    "</table>";

    when this script is executed, it's through AJAX (xmlHttp) and it returns the sql statement without the 'wordpress' table prefix, and I'm not sure what todo here. here's the output:

    HTML Code:
    SELECT * FROM event ORDER BY date_added ASC
    does anyone know why this is doing what it is? I've tried putting the sql statement directly into the get_results method, and that doesn't help... Also, if I put the echo $sql statement in an else condition after the if, it won't show up at all...

    another note, I've been using the global in other PHP files in the same directory just fine. Does it being called through the AJAX functions have anything todo with it?

    Any help would be great, thanks!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do you include any of the wordpress library to initialise the global in question?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist rhysboy84's Avatar
    Join Date
    May 2007
    Location
    Colwyn Bay, North Wales, UK
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is this function called? You need to hook it onto something with wordpress to begin with, if you haven't already done so
    I'm Rhys Wynne & I blog at Winwar Media
    WP Email Capture: Free Email/Ebook Marketing Wordpress Plugin
    UK Based SEO? Tweet Your Location to #ukseohere!
    | My Brand New Brand | Twitter |

  4. #4
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @silverbullet: No, I haven't. Didn't think I needed to. On other pages in the same directory all I did was call the 'global $wpdb', and it worked just fine. It's only in the this file that it gives me the trouble. How would I initialize it if I needed to?

    @rhyboy: it's called off of a drop down list. When ever the value of the list changes, it call a JS function "showEvents()" which grabs the data from the php file (listed above) and then returns the output of that file (the database results) to the <div id="events"></div>. the page that the <div id='events'> is hooked through the plugins setup file... (make sense?)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •