SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pass dot in PHP to SQL query

    I have a variable:

    $orderby = orders.name;

    $result = mysql_query("SELECT * FROM orders,categories WHERE orders.catid = categories.id AND orders.complete = '$complete' ORDER BY $orderby DESC");

    Since I can't have the dot in $orderby. How can I pass it to the query, so that it orders by orders.name etc?

    I am planning to use it in a query string, so should I use: admin.php?orderby=orders_name instead of orders.name? And how can I get the query formated correctly and use orders.name?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,404
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    $orderby = 'orders.name';

    You forgot to put the quotes around the string value 'orders.name'

  3. #3
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try $orderby like this:
    PHP Code:
    $orderby '`orders`.`name`'
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your responses.
    Can I use a dot in the URL like this admin.php?orderby=orders.name ?
    I noticed scripts like wordpress use an underscore

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,404
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Try it

    If it doesn't work, take a look at http://www.php.net/urlencode

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can use dot in URL like that.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Norway
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried using a dot and it works good, but I have seen others are using underscores etc instead. Is it bad practice to use dots in the URL?

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I use "-" as delimiter between words, so I get URL like "http://domain/some-interesting-article". It is easily readable.

    Anyway, in this particular case you are creating URL for administration, so it does not matter what is in the URL, if the delimiter is "." or something else.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •