• Resolved sonnensegler

    (@sonnensegler)


    Hey there,
    I need to run a simple SQL query on the bookings database wp_em_bookings of the plugin Events Manager. I am using the newest version of both WordPress and the plugin.

    Say, I want to check the booking status of a specific user and echo it:

    <?php
    global $wpdb;
    global $EM_Event;
    global $current_user;
    get_currentuserinfo();
    $userid = $current_user->ID;
    $eventid = $EM_Event->id; 
    
    $mybookings = $wpdb->get_results("SELECT * FROM $wpdb->em_bookings WHERE event_id = $eventid AND person_id = $userid");
    
    foreach ($mybookings as $mybooking) {
    	echo $mybooking->booking_status;
    }
    ?>

    But whatever I’m using, either $wpdb->query or get_results, I can’t get my query to access the database tables. I tried to replace $wpdb->em_bookings with ‘$wpdb->prefix . EM_BOOKINGS_TABLE;” (found this in another forum post), but still no result. I tried adding the output_type for an array, nothing.

    I even replaced all variables with actual numbers, id’s existing in the table. Nothing.

    If I output only the variables $userid and $eventid from above, it’s giving me the right numbers. If I run a SELECT query on a normal wordpress database table, e.g. wp_links, it’s working as well.

    Do I need to set a different global variable??

    What am I doing wrong?

    Help greatly appreciated!

    Thanks, Christiane

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Marcus (aka @msykes)

    (@netweblogic)

    are you checking for sql errors?

    Thread Starter sonnensegler

    (@sonnensegler)

    Yes, I finally did within SequelPro (and with Firebug and activated php error logging) and that saved me a lot of time ??

    Fortunately I got it working with

    $results = $wpdb->get_results(“SELECT * FROM wp_em_bookings WHERE event_id = $eventid AND person_id = $userid “, OBJECT_K);

    (the WordPress tutorial for interfacing with the database tells you to leave out the prefix)

    and then working with that array. Plus I had to add

    define(‘WP_USE_THEMES’, false);
    require($_SERVER[‘DOCUMENT_ROOT’].’/wp-blog-header.php’);

    in the header of my file since my function doesn’t get all the globals from WordPress headers.

    A client of ours needs a possibility to cancel an event straight away without ever reserving for it, so I had to try a little with database and booking status.

    I solved the core changing problem by using a custom placeholder for this special button.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘SQL query on bookings database’ is closed to new replies.