Support » Plugin: PHP Code for posts » Alias for table – wrong result

  • I have the impression that aliasses for tables give a strange result. See code under.
    Both statements are valid in Mysql, but not in PHP Code

    The first statement gives a result:
    35 Kameroen

    And the second:
    Travelid Travel

    which are the headers.

    Thanks,
    Herman

    <?php

    global $wpdb;

    $sql = “SELECT TravelId, Travel
    FROM tr_Travel
    WHERE tr_Travel.TravelId = 35″;

    Echo “
    </BR>”.$sql;

    $r = $wpdb->get_row($sql);

    Echo “
    </BR>”.$r->TravelId.” “.$r->Travel;

    $sql = “SELECT t.TravelId, t.Travel
    FROM tr_Travel t
    WHERE t.TravelId = 35″;

    Echo “
    </BR>”.$sql;

    $r = $wpdb->get_row($sql);

    Echo “
    </BR>”.$r->t.TravelId.” “.$r->t.Travel;

    ?>

    https://wordpress.org/plugins/php-code-for-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jamie Fraser

    (@thejfraser)

    Hi Herman,

    This is certainly an interesting one;

    You echo both $sql variables after you assign them, what are the values that are output?

    Hi Jamie,
    Yes interesting and I need an alias because i’m reading my country table twice in the query.

    I can’t attach a screenshot here?
    But you can see the output on: http://www.test.lamboo.com/a/

    Thanks,
    Herman

    Plugin Author Jamie Fraser

    (@thejfraser)

    Hi Herman,

    It looks like the plugin isn’t modifying your statement in any negative way, it could be an issue with wpdb or your version of mysql itself;

    the plugin is passing SELECT t.TravelId, t.Travel FROM tr_Travel t WHERE t.TravelId = 35 to the second query.

    Does it work if you explicitly use the AS keyword?

    SELECT t.TravelId, t.Travel FROM tr_Travel AS t WHERE t.TravelId = 35

    Hi Jamie,
    No, I tried the AS before. Naming the alias is not the problem, just referencing to it. Strange. I don’t know for know and will use a workaround, just 2 queries.
    Thanks a lot for searching.

    Herman

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Alias for table – wrong result’ is closed to new replies.