• I’m trying to create a plugin for use on our multisite install, but I can’t quite figure out how to the db query in a way that will automatically get data from the correct subsite’s db tables.

    Here’s a query that works:

    $reportList = $wpdb->get_results($wpdb->prepare("SELECT wp_2_watupro_taken_exams.ID,
    wp_2_watupro_taken_exams.exam_id,
    wp_2_watupro_taken_exams.user_id,
    wp_2_watupro_taken_exams.end_time 'completed',
    wp_2_watupro_master.name 'assessment',
    wp_users.display_name 'name',
    wp_users.user_email 'email'
    FROM wp_2_watupro_taken_exams
    JOIN wp_2_watupro_master ON wp_2_watupro_master.ID = wp_2_watupro_taken_exams.exam_id
    JOIN wp_users ON wp_users.ID = wp_2_watupro_taken_exams.user_id
    WHERE wp_users.user_email LIKE '%".$_POST['client']."%'
    ORDER BY wp_users.display_name;"));

    I want to make it so that I don’t have the “wp_2_” in there — i.e., so that it will just automatically get the results based on the right prefix for the current site.

    I’ve done a bunch of searching and reading, and here’s what I’ve got (which doesn’t work):

    $reportList = $wpdb->get_results($wpdb->prepare("SELECT $wpdb->watupro_taken_exams.ID,
    $wpdb->watupro_taken_exams.exam_id,
    $wpdb->watupro_taken_exams.user_id,
    $wpdb->watupro_taken_exams.end_time 'completed',
    $wpdb->watupro_master.name 'assessment',
    $wpdb->users.display_name 'name',
    $wpdb->users.user_email 'email'
    FROM $wpdb->watupro_taken_exams
    JOIN $wpdb->watupro_master ON $wpdb->watupro_master.ID = $wpdb->watupro_taken_exams.exam_id
    JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->watupro_taken_exams.user_id
    WHERE $wpdb->users.user_email LIKE '%".$_POST['client']."%'
    ORDER BY $wpdb->users.display_name;"));

    So, the question is – where have I gone wrong?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You should use $wpdb->prefix, it’s automatically figured out for every site as long as the plugin is activated as blog admin.

    Thread Starter Scott McCulloch

    (@sinemac)

    Thanks @prasunsen

    I’ve seen $wpdb->prefix done a few different ways… none of them worked for me… here are some examples… just snippets to show how – a couple of these I just tried out of desperation ??

    {$wpdb->prefix}watupro_taken_exams.ID

    $wpdb->prefix_watupro_taken_exams.ID

    $wpdb->prefix->watupro_taken_exams.ID

    $wpdb->prefix.watupro_taken_exams.ID

    ".$wpdb->prefix." watupro_taken_exams.ID

    So, I think I need a little more help… I don’t even know if I was close on any of those.

    Thread Starter Scott McCulloch

    (@sinemac)

    I did get close! Here’s what worked:

    ".$wpdb->prefix."watupro_taken_exams.ID

    Done in by a space!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to write query for multisite use’ is closed to new replies.