Search code examples
phpsqldatabaseprefix

How to define table prefix in variable


I want to define prefix in variable

global $wpdb;
    $prefix = 'qa_';
    $titles = $wpdb->get_results("
        SELECT title
        FROM "$prefixpages"
        }
    ");

Can anyone guide why its not working and hot to make it works.


Solution

  • if you need to query from table qa_tages:

    global $wpdb;
    $prefix = 'qa_';
    $titles = $wpdb->get_results("
        SELECT title
        FROM ${prefix}pages
        }
    ");
    

    OR same line:

        ..."
        FROM ".$prefix."pages
        "...
    

    if you need contents of $qa_pages variable:

    global $wpdb;
    $prefix = 'qa_';
    $titles = $wpdb->get_results("
        SELECT title
        FROM ".${$prefix.'pages'}."
        }
    ");