Page 1 of 1

Can't account for spaces in my database when echoing out dat

Posted: Tue May 29, 2012 9:04 am
by mcc_shane
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

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]

Re: Can't account for spaces in my database when echoing out

Posted: Tue May 29, 2012 11:23 am
by pickle
Do a var_dump() of $city and see what that looks like. urldecode() is applicable here, so I'm not sure exactly what the issue might be.

Re: Can't account for spaces in my database when echoing out

Posted: Tue May 29, 2012 12:00 pm
by mikosiko
ctype_alnum() count blank spaces or special characters like dash?

http://php.net/manual/en/function.ctype-alnum.php

Re: Can't account for spaces in my database when echoing out

Posted: Tue May 29, 2012 2:21 pm
by social_experiment
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.
http://se2.php.net/manual/en/function.urldecode.php
Also the last URL does return a value if i visit the page (show's Las Vegas - not sure if you might have fixed it.);

Re: Can't account for spaces in my database when echoing out

Posted: Wed May 30, 2012 3:57 am
by Grizzzzzzzzzz
looks like you may have fixed it with Las-Vegas.

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.