Error SQL when use in the query the fields from or to in table mb_relationships

Support MB Relationships Error SQL when use in the query the fields from or to in table mb_relationshipsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14878
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    I have the table mb_relationships created, and if I try to make a query by sql, if I use the fields from or to I have a error:

    If I use the field ID or the field type, the search is ok:
    ok-> $resultado = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE type = 'opinion_to_atraccion'"));
    ok-> $resultado = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE ID = '22'") );

    But if in the query I use from or to, is a error..Maybe this two words have a conflict with the wordpress database?
    Error-> $resultado = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE from = $ID"));
    Error en la base de datos de WordPress: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from = 1635' at line 1]

    Error-> $resultado = $wpdb->get_row($wpdb->prepare("SELECT to FROM $table WHERE from = $ID"));
    Error en la base de datos de WordPress: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'to FROM w47fa_mb_relationships WHERE from = 1635' at line 1]
    SELECT to FROM w47fa_mb_relationships WHERE from = 1635

    Thanks,
    Sergio

    #14883
    Anh TranAnh Tran
    Keymaster

    Hi Sergio,

    The problem is from is a reserved keyword for MySQL. You can't use it directly in your SQL. To get over it, you need to put it in back ticks (`). Here is the modified SQL:

    https://pastebin.com/d8RUsspx

    (Can't post the SQL here because back ticks are used to insert code in this forum)

    #14885
    proyectohappyweb@gmail.com[email protected]
    Participant

    Perfect!!! It run well!!!
    Many thanks

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.