Can't account for spaces in my database when echoing out dat
Posted: Tue May 29, 2012 9:04 am
Hi Everyone,
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!
http://whatsmyowncarworth.com/auto/miami
http://whatsmyowncarworth.com/auto/providence
http://whatsmyowncarworth.com/auto/albany
http://whatsmyowncarworth.com/auto/boston
http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name
http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.htaccess
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!
http://whatsmyowncarworth.com/auto/miami
http://whatsmyowncarworth.com/auto/providence
http://whatsmyowncarworth.com/auto/albany
http://whatsmyowncarworth.com/auto/boston
http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name
http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name
Code: Select all
<?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"];
}
}
}
}
?>.htaccess
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /auto/cars.php?u=$1 [NC]