• I have a sql query that returns no data. I have made these queries 1000’s of times. I am hoping for a little input here as our functions file is over 3000 lines long.

    Why would the first query not function but the second one does?

    This query returns a blank result (I have MANY queries that do work with the same format (” around the variables)):

    $companyid = $wpdb->get_var($wpdb->prepare(“SELECT business_id FROM $usertable WHERE $id = %d”, $uid));

    This query returns the correct result (removed the ” around the variables):

    $companyid = $wpdb->get_var($wpdb->prepare(“SELECT business_id FROM ‘$usertable’ WHERE ‘$id’ = %d”, $uid));

Viewing 1 replies (of 1 total)
  • Becuase in MySQL the single-quote is for referneceing values, not table/column names. That means that when you have WHERE '$id' in there it’s taking the $id value as a value of a column, not a column name.

    You’ve already found the anser ot this – don’t put single-quotes around the column names, only the values that are found in columns.

    Example…

    SELECT col_1, col_2 FROM table_name WHERE col_3 = 'value' ORDER BY id ASC

Viewing 1 replies (of 1 total)

The topic ‘Functions.php issue Sql Query’ is closed to new replies.