PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
What I'm trying to do is echo out information from a mysql database into my URL. I have done that (please see below code) but what I haven't figured out yet is for the code to handle spaces or two words. For example, if you visit the below URLs the page will echo out the city name (which is what I want) but I have a city called "Las Vegas" in my mysql database which is obviously two words and when I type in las vegas, las-vegas or Las Vegas etc ... at the end of the URL the code isn't echoing out anything. I also just recently added a urldecode within my code to see if that works but it appears it isn't. What am I doing wrong? What do I have to change in my syntax? Thanks everyone!
<?php
include('init.php'); // connection to database
if (isset($_GET['u'])) {
$city = mysql_real_escape_string(urldecode($_GET['u']));
// protection against mysql injection
if (ctype_alnum($city)) {
$data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
if (mysql_num_rows($data) > 0) {
while ($row = mysql_fetch_assoc($data)) {
echo $row["City"];
}
}
}
}
?>
Not sure if this is useful but from the php manual
The Manual wrote:
The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
If you're still deciding on a final way to handle it, from an SEO perspective go the route of using -'s as spaces, it's processed more naturally by search engines.