url variable

Discussion in 'PHP' started by cn45896, Aug 12, 2011.

  1. #1
    It's been a while since I have done any coding and have a url that looks like:
    /products/some-productname

    Theres a rewite rule that reads that last folder as a get variable like $_GET['product'] = "value"

    I want to query the DB on that value, but when it has a dash in it, it does not function like I want it to, although I want to make it readable by having dashes in there.

    Whats the best way to handle so that I can get the WHERE productSlug = "$product"?
     
    cn45896, Aug 12, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    WHERE productSlug = str_replace("-"," ",$product); // try single quotes if this doesn't work, remember to leave a space to replace - with an empty space
    PHP:
    Failing the above, alter $product before the query:

    
    $product = str_replace("-"," ",$product); // there's no effect unless the product name has a dash
    WHERE productSlug = "$product";
    PHP:
    Example test.php
    
    <?php 
    
    $product = "some-productname";
    
    echo $product; // some-productname
    
    echo "<br / >";
    
    $product = str_replace("-"," ",$product);
    
    echo $product; // some productname
    
    ?>
    
    PHP:
    OUT_PUT:

    
    some-productname
    some productname
    
    Code (markup):
     
    Last edited: Aug 12, 2011
    MyVodaFone, Aug 12, 2011 IP
  3. cn45896

    cn45896 Guest

    Messages:
    140
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks guys. I just stripped out the spaces and dashes, but I wanted to have it look more readable than that, but just cant get it to work with my query. Im good.
     
    cn45896, Aug 16, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Are you quoting the string? ('-myproduct', not myproduct, within the quoted string? IOW, it should look like "blah blah where foo = '-bar';")

    You could also try "where foo like '-bar%';"

    You might also try mysql_real_escape_string().

    What will work depends more on the database than on anything else.
     
    Rukbat, Aug 22, 2011 IP