• The piece of code below has me puzzled to no extend. I’d love to find a solution to the ampersand issue.

    The code below works fine, pulling a post title in and looking up data in a custom table (inside the wordpress database) …. HOWEVER …. if the post title contains an Ampersand (&) the code breaks.

    The really annoying thing is that when I enter the post-title (e.g. bacon & eggs) directly into the SELECT statement below (instead of the variable $bedrijftitel) , the code DOES work.
    I’m sure it’s staring me in the face but the solution has eluded me so far …. Your help would be much appreciated.

    // get bedrijfsdata from custom table
    $bedrijftitel = get_the_title();
    $bedrijfsinfo_uit_db = $wpdb->get_row('SELECT * FROM bedrijfsinfo WHERE bedrijfsnaam = "'. $bedrijftitel . '" ');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Does using $wpdb->prepare fix the problem?

    Thread Starter owagu

    (@owagu)

    no, the prepare function does not help.
    The problem seems to be with the get_the_title() function.

    this does NOT work

    // get bedrijfsdata from custom table
    $bedrijftitel = get_the_title();
    $bedrijfsinfo_uit_db = $wpdb->get_row("SELECT * FROM bedrijfsinfo WHERE bedrijfsnaam = '$bedrijftitel' ");

    this DOES work

    // get bedrijfsdata from custom table
    $bedrijftitel = 'steel & stainless';
    $bedrijfsinfo_uit_db = $wpdb->get_row("SELECT * FROM bedrijfsinfo WHERE bedrijfsnaam = '$bedrijftitel' ");

    the database record for this title is : steel & a m p ; stainless

    Duhhh ….. disregard the above … While writing this I found the answer : html_entity_decode

    $bedrijftitel = html_entity_decode(get_the_title() ); converts the title back to it’s original with ampersand character.

    (sometimes it helps when you talk to yourself ?? ).

    (sometimes it helps when you talk to yourself ?? ).

    Yes. Yes it does. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘ampersands $wpdb and headaches’ is closed to new replies.