Support » Fixing WordPress » Connect to non-WordPress db

  • Resolved chrismec


    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');
    // Get current WP user info
    // 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'"));
Viewing 4 replies - 1 through 4 (of 4 total)
  • 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…

    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 ...

    No… Didn’t work. 🙁

    Try this for the proper code –

    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Connect to non-WordPress db’ is closed to new replies.