WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Connect to non-Wordpress db (5 posts)

  1. chrismec
    Member
    Posted 1 year ago #

    Hello all, newb here. Trying to connect to a non-wordpress database and getting a syntax error. Have tried everything I can think of. Any advice?

    global $wpdb;
    $Pagedb = new wpdb('someUser','somePass','someName','localhost');
    $Pagedb->show_errors();
    
    // Get current WP user info
    get_currentuserinfo();
    
    // Get current users dept email
    $subDept = $wpdb->get_var( "SELECT dept_email FROM $wpdb->users" );
    
    // Lookup subscribers database table based on dept_email
    //$subTable = $Pagedb->get_var($Pagedb->prepare("SELECT Table FROM $Pagedb->FP_Emails WHERE $Email = '$subDept'"));
  2. chrismec
    Member
    Posted 1 year ago #

    Sorry, posted that in a hurry. Had to get the kids a bath. The syntax error is an SQL syntax error. A portion of the error:
    for the right syntax to use near 'Table FROM WHERE = '

    I do have valid uwer pass and name in there too. Have tried many variations on the FROM field but nothing seems to work...

  3. catacaustic
    Member
    Posted 1 year ago #

    You're getting that message becuase "table" is a reserved keyword in MySQL and you haven't specified any columns to select. If you're going to do this the right way you should rename that table. Using any reserved keywords not only causes these issues, it also adds a lot of confusion for when you go back to look at the code in 6 months time and have forgotten what you did all the way back then (happens all the time!!!).

    If you really have to keep "Table" as the table name, then you can escape it with back-ticks and MySQL will take it as a literal table name and not a reserved word. I'll try to post the back-ticks here, but they are used for quoting code so I'm not sure if it will work... if not, it's the character on most keyboards that's to the left of '1'.

    SELECT col1, col2, col3 FROM \Table\WHERE ...

  4. catacaustic
    Member
    Posted 1 year ago #

    No... Didn't work. :(

    Try this for the proper code - http://wordpress.pastebin.ca/2335520

  5. chrismec
    Member
    Posted 1 year ago #

    YES! Thank you! Temporary back-ticks and removing the variable symbol before Email did the trick. I will change the column's name from Table to something else. Your the best!

Topic Closed

This topic has been closed to new replies.

About this Topic